Why does $(\frac{p-1}{2}!)^2 = (-1)^{\frac{p+1}{2}}$ mod $p$?

This question already has an answer here:

  • Show for prime numbers of the form $p=4n+1$, $x=(2n)!$ solves the congruence $x^2\equiv-1 \pmod p$. $p$ is therefore not a gaussian prime.

    2 answers

Solutions Collecting From Web of "Why does $(\frac{p-1}{2}!)^2 = (-1)^{\frac{p+1}{2}}$ mod $p$?"

$$(p-1)!=1\cdot2\cdots\frac{p-1}{2}\cdot\frac{p+1}{2}\cdots(p-2)(p-1)$$ We have the congruences
$$\begin{align*}
p-1&\equiv -1\pmod p\\
p-2&\equiv -2\pmod p\\
&\vdots\\
\frac{p+1}{2}&\equiv -\frac{p-1}{2}\pmod{p}\end{align*}$$
Rearranging the factors produces

$$(p-1)!\equiv 1\cdot(-1)\cdot2\cdot(-2)\cdots\frac{p-1}{2}\cdot\left(-\frac{p-1}{2}\right)\pmod p.$$
$\therefore (p-1)!\equiv (-1)^{\frac{p-1}{2}}\left(1\cdot2\cdots\frac{p-1}{2}\right)^{2}\pmod p.$
From Wilson’s theorem, $(p-1)!\equiv -1\pmod{p}$
Thus $$-1\equiv (-1)^{\frac{p-1}{2}}\left[\left(\frac{p-1}{2}\right)!\right]^{2}\pmod{p}$$
It follows that $\left[\left(\frac{p-1}{2}\right)!\right]^{2}\equiv (-1)^{\frac{p+1}{2}}\pmod{p}$.