Why is the square of all odds less than an odd prime $p$ congruent to $(-1)^{(p+1)/(2)}\pmod p$?

I came across the two congruences that for $p$ an odd prime,

$$1^2\cdot 3^2\cdot 5^2\cdots (p-2)^2\equiv (-1)^{(p+1)/(2)}\pmod p$$

and

$$2^2\cdot 4^2\cdot 6^2\cdots (p-1)^2\equiv (-1)^{(p+1)/(2)}\pmod p$$

What is the reason for this? It seems to me to be closely related to Wilson's Theorem, and possibly Fermat's little theorem since $p-1=2k$ for some $k$, and then $j^{2k}\equiv 1\pmod p$ for all the evens and odds $j$ less than $p$, but I wasn't able to complete this train of thought.

-

Modulo $p$, you have $k=-(p-k)$ and so both products are equal to $(p-1)!(-1)^{(p-1)/2} \equiv (-1)^{(p+1)/2} \pmod{p}$ by Wilson's theorem.

-
Pardon me if I'm mistaken, but did you mean to say modulo $p$, $k=k-p$? Then I see that $1^2\cdot 2^2\cdot 3^2\cdots (p-2)^2\equiv (1)(1-p)(2)(2-p)\cdots (p-2)(-2)\equiv (p-1)!(-1)^{(p-1)/2}\pmod p$. – yunone Sep 17 '10 at 2:22
Got me! oops... – Yuval Filmus Sep 17 '10 at 3:22
Regardless, thank you for pointing me in the right direction. – yunone Sep 17 '10 at 4:18

I amplify the details -

The crux is to use $\color{purple}{ p-k\equiv -k \mod p }$.

$[ 2 \cdot 4 ... (p - 3) \cdot (p-1)] \cdot \quad \color{purple}{[ 2 \cdot 4 ... (p - 3) \cdot (p-1)]}$ $\equiv [ 2 \cdot 4 ... (p - 3) \cdot (p-1)] \cdot \quad \color{purple}{[ (p-2) \cdot (p-4) ... \cdot (1) \quad \cdot (-1)^\frac{p-1}{2}]} (\mod p) = (p - 1)! \quad \cdot (-1)^\frac{p-1}{2} \equiv -1 \quad \cdot (-1)^\frac{p-1}{2} \quad (mod p)$

The last line is by reason of Wilson's Theorem.

-