Example 5 (10th Turkish Mathematical Olympiad, 2002) Find all prime numbers $p$ such that there are exactly $p$ pairs of integers $(x, y)$ satisfying $0 \leqslant x, y \leqslant p$ and $y^{2} \equiv x^{3}-x(\bmod p)$.
We introduce the Legendre symbol $\left(\frac{a}{p}\right)$:
$p$ is an odd prime, $(a, p)=1$;
$\left(\frac{a}{p}\right)=\left\{\begin{array}{l}1, \text { when } x^{2} \equiv a(\bmod p) \text { has a solution, } \\ -1, \text { when } x^{2} \equiv a(\bmod p) \text { has no solution. }\end{array}\right.$
Notice the following properties of the Legendre symbol:
(1) $\left(\frac{a}{p}\right) \cdot\left(\frac{b}{p}\right)=\left(\frac{a b}{p}\right)$,
(2) $\left(\frac{-1}{p}\right)=\left\{\begin{array}{l}1, p \equiv 1(\bmod 4) \\ -1, p \equiv-1(\bmod 4)\end{array}\right.$
Also notice Theorem 1, i.e., the following conclusion:
If $(a, p)=1, p$ is an odd prime, and $x^{2} \equiv a(\bmod p)$ has a solution, then it has exactly two solutions.
In fact, if $x \equiv x_{0}$ is a solution, then $x \equiv-x_{0}$ is also a solution. Since $p$ is odd, then $x_{0} \neq -x_{0}(\bmod p)$. Therefore, there are at least two solutions.
If there is another solution $x_{1}$, and $x_{1} \neq x_{0}, x_{1} \neq-x_{0}(\bmod p)$, then $x_{0}^{2} \equiv x_{1}^{2}(\bmod p)$,
$\left(x_{0}-x_{1}\right)\left(x_{0}+x_{1}\right) \equiv 0(\bmod p)$.
Thus, $p \mid\left(x_{0}-x_{1}\right)$ or $p \mid\left(x_{0}+x_{1}\right)$. This is a contradiction.
Therefore, there are only two solutions.
Now we prove the original problem.
Let $f(x)=x^{3}-x$, then $f(x)$ is an odd function.
When $p=2$, $y^{2} \equiv f(x)(\bmod p)$ has exactly two solutions $(0,0),(1,0)$.
When $p \equiv 3(\bmod 4)$, since $\left(\frac{-a}{p}\right)=\left(\frac{-1}{p}\right)\left(\frac{a}{p}\right)=-\left(\frac{a}{p}\right)$, therefore,
$y^{2} \equiv f(x)$ and $y^{2} \equiv-f(x) \equiv f(-x)$ have one solution and one no solution.
Assume that for $x=2,3, \cdots, \frac{p-1}{2}$, $y^{2} \equiv f(x)$ has $k$ solutions, then for $x=\frac{p+1}{2}, \frac{p+3}{2}, \cdots, p-2$, $y^{2} \equiv f(x)$ has $\frac{p-3}{2}-k$ solutions.
Therefore, for $x=2,3, \cdots, p-2$, $y^{2} \equiv f(x)$ has $\frac{p-3}{2}$ solutions.
Since each solution has two solutions, then for $x=2,3, \cdots, p-2$, there are $p-3$ pairs of solutions.
When $x=0,1, p-1$, $y^{2} \equiv f(x) \equiv 0(\bmod p)$ each has one pair of solutions.
Therefore, there are $p$ pairs of solutions in total.
When $p \equiv 1(\bmod 4)$,
since $\left(\frac{-a}{p}\right)=\left(\frac{-1}{p}\right)\left(\frac{a}{p}\right)=\left(\frac{a}{p}\right)$, therefore, $y^{2} \equiv f(x)$ and $y^{2} \equiv f(-x)$ either both have two solutions or both have no solutions.

Assume that for $x=2,3, \cdots, \frac{p-1}{2}$, $y^{2} \equiv f(x)$ has $k$ solutions, then for $x=\frac{p+1}{2}, \frac{p+3}{2}, \cdots, p-2$, $y^{2} \equiv f(x)$ has $k$ solutions.
Therefore, for $x=2,3, \cdots, p-2$, $y^{2} \equiv f(x)(\bmod p)$ has $4k$ pairs of solutions.
When $x=0,1, p-1$, $y^{2} \equiv f(x) \equiv 0(\bmod p)$ each has one pair of solutions.
Therefore, there are $4k+3$ pairs of solutions in total.
But $p \neq 4k+3$, so $p \equiv 1(\bmod 4)$ must not satisfy the condition.
In summary, $p=2$ or $p \equiv 3(\bmod 4)$.