TITLE: The square root of Wilson's theorem when $p\equiv 1 \mod 4$
QUESTION [20 upvotes]: My question relates, at least superficially, to these old ones:
The value $\pm 1$ for the square root of Wilson's theorem, ((p-1)/2)! mod p
Primes P such that ((P-1)/2)!=1 mod P
When $p\equiv 1 \mod 4$, if $x=((p-1)/2)!$, then $x^2 = -1 \mod p$.
For what primes does $x \in \{1,\ldots,(p-1)/2\}$ (the elements of the set regarded as residues $\mod p$)?   
One gets "yes" for $5,13,29,41,53,61,73,89,97,\ldots$ and "no" for 
 $17,37,101,\ldots$.  Despite the slow start for "no", the counts substantially even out, say, when looking at primes up to 100000.  Can one prove that the ratio approaches $1/2$?

REPLY [20 votes]: In the case $p \equiv 1$ mod $4$, the connection is to the real quadratic field ${\mathbb Q}(\sqrt{p})$, whereas the case $p \equiv 3$ mod $4$ is connected to the imaginary quadratic field ${\mathbb Q}(\sqrt{-p})$.  
Chowla ("On the class number of real quadratic fields", 1961 PNAS) proves that
$$\left( \frac{p-1}{2} \right)! \equiv (-1)^{\frac{h+1}{2}} \cdot \frac{t}{2} \text{ mod } p,$$
where $h$ is the class number of ${\mathbb Q}(\sqrt{p})$ and the fundamental unit is $\frac{1}{2}(t + u \sqrt{p}) > 1$ with $t,u \in {\mathbb Z}$.  
In fact, 0 < t < 2p.  EDIT:  that's false.  I think I was remembering a converse, that if you find such an element $\frac{1}{2}(t + u \sqrt{p})$ of norm $-1$, and $0 < t < 2p$, then it's a fundamental unit.  (This is right, I hope, but not so relevant).  See Upper bound on answer for Pell equation for more on bounding $t$.
So it seems like the answer depends on (1) the class number mod 4 and (2) whether $\frac{t}{2} \in \{ 1,2,\ldots,\frac{p-1}{2} \}$ mod $p$.  The statistics seem (to me) at least as difficult as the $p \equiv 3$ mod $4$ case.