Given $n$ is a positive odd integer what is the fastest way to compute $\left(\frac{n-1}{2}\right)!\pmod{n}$ ?
I've observed that:
$2 \cdot \left(\frac{n-1}{2}\right) \equiv -1 \pmod{n}$
$3 \cdot \left(\frac{n-1}{2} - 1\right) \equiv \frac{3n-9}{2} \pmod{n}$
$4 \cdot \left(\frac{n-1}{2} - 2\right) \equiv -10 \pmod{n}$
And therefore:
$2 \cdot 3 \cdot 4 \cdot \left(\frac{n-1}{2}\right) \cdot \left(\frac{n-1}{2} - 1\right) \cdot \left(\frac{n-1}{2} - 2\right) \equiv (-1) \cdot \left(\frac{3n-9}{2}\right) \cdot (-10) \equiv -45 \pmod{n}$
So this reduces the number of multiplications needed by $5$ and it can be reduced even more if we continue multiplying the numbers like that. But I don't know how to get a formula from it. Is it possible at all?
Assume $n$ is odd, composite, and not the square of a prime. Then $n=dD$ with $1<d<D<n/2$. $d$ and $D$ are both represented in the product $\left ( \frac{n-1}{2} \right )!$. So the result is zero.
Assume now $n$ is the the square of a prime $p$, then you need $2p \leq \frac{p^2-1}{2}$ to draw the same conclusion, by ensuring that $p$ and $2p$ are both represented in the product $\left ( \frac{n-1}{2} \right )!$. For positive integer $p$ this is equivalent to $p^2-4p>0$ so $p>4$. So the result is zero for squares of all odd primes except $3$.
Overall this tells us that the result is zero if $n$ is any odd composite except $9$.
It's easy enough to calculate that $4! \equiv -3 \pmod 9$, so that case can be handled by direct calculation. For prime $n$ I don't know a fast way to do it (i.e. faster than $O(n)$). In fact I am skeptical that there is a way to do this much faster than $O(n)$ (e.g. polynomial in $\log(n)$), because if there were then this would be a good primality test.