TITLE: Primes P such that ((P-1)/2)!=1 mod P
QUESTION [39 upvotes]: I was looking at Wilson's theorem: If $P$ is a prime then $(P-1)!\equiv -1\pmod P$. I realized this
implies that for primes $P\equiv 3\pmod 4$, that $\left(\frac{P-1}{2}\right)!\equiv \pm1 \pmod P$. 
Question: For which primes $P$ is $\left(\frac{P-1}{2}\right)!\equiv 1\pmod P$?
After convincing myself that it's not a congruence condition for $P,$ I found this sequence in OEIS. I'd appreciate any comments that shed light on the nature of such primes (for example, they appear to be of density 1/2 in all primes that are $3\bmod 4$).
Thanks,
Jacob

REPLY [5 votes]: Apologies for repeating some information in my reply to question 121678, which I came across before seeing this one.
Several previous answers already explain the connection to the class number. It can be added that the value of $h(-p)$ was investigated by Louis C. Karpinski in his doctoral dissertation (Mathematischen und Naturwissenschaftlichen Facultät der Kaiser Wilhelms-Universität zu Strassburg, 1903), published as “Über die Verteilung der quadratischen Reste,” Journal für die Reine und Angewandte Mathematik 127 (1904): 1–19. Karpinski proved a collection of formulae (all of which assume $p > 3$) involving sums over Legendre symbols, and showed that the most concise sums possible contain only $\lfloor p/6 \rfloor$ terms:
\begin{equation}
\left\{ 2 - \left( \frac{2}{p} \right) \right\} h(-p) = \sum_{k=1}^{(p-1)/2} \left( \frac{k}{p} \right) \quad (p \equiv 3 \bmod{4});
\end{equation}
\begin{equation}
\left\{ 3 - \left( \frac{3}{p} \right) \right\} h(-p) = 2 \sum_{k=1}^{\lfloor p/3 \rfloor} \left( \frac{k}{p} \right) \quad (p \equiv 3 \bmod{4});
\end{equation}
\begin{equation}
\left\{ 2 - \left( \frac{2}{p} \right) \right\} h(-p) = \sum_{k=\lfloor p/4 \rfloor +1}^{(p-1)/2} \left( \frac{k}{p} \right) (p \equiv 3 \bmod{8});
\end{equation}
\begin{equation}
\left\{ 2 - \left( \frac{2}{p} \right) \right\} h(-p) = \quad \sum_{k=1}^{\lfloor p/4 \rfloor} \quad \left( \frac{k}{p} \right) (p \equiv 7 \bmod{8});
\end{equation}
\begin{equation}
\left\{ 1 + \left( \frac{2}{p} \right) + \left( \frac{3}{p} \right) - \left( \frac{6}{p} \right) \right\} h(-p) =  2 \sum_{k=1}^{\lfloor p/6 \rfloor} \left( \frac{k}{p} \right) \quad (p \equiv 7, 11, 23 \bmod{24});
\end{equation}
\begin{equation}
\left\{ 1 + \left( \frac{2}{p} \right) + \left( \frac{3}{p} \right) - \left( \frac{6}{p} \right) \right\} h(-p) = -2p  + 2 \sum_{k=1}^{\lfloor p/6 \rfloor} \left( \frac{k}{p} \right) \quad (p \equiv 19 \bmod{24}).
\end{equation}