TITLE: how many $2\times2$ matrices exist with this condition?
QUESTION [7 upvotes]: Let $p=10007$ which is prime. I want to find the number of matrices X of $2\times2$ dimension with elements from $\mathbb{Z}_p$ for which $X^2\equiv I$ (mod $p$). Where $I$ - identity matrix. How can I solve this problem? I tried to write down the square of matrix so that I get the system of congruences, but I can't figure out what to do after that, and I don't think this is the right way to solve this.

REPLY [5 votes]: As in @ThomasAndrews' comment, there is indeed a systematic (not-so-computational) way to approach this, maybe less lending itself to computational errors and such.
So, for a two-by-two matrix with $x^2=1$, certainly $x^2-1=0$, so either $x-1=0$, or $x+1=0$, or neither holds, but $x^2-1=0$.
In the first two cases, $x$ is $\pm$ the identity matrix, that is, has either eigenvalue $1$ (with multiplicity $2$) or eigenvalue $-1$ (with multiplicity $2$).
In the third case, necessarily $x$ has eigenvalues $1$ and $-1$. By standard linear algebra (!!!), this means that there is an invertible two-by-two matrix $A$ such that
$$
x \;=\; Ax_oA^{-1}
$$
with
$$
x_o \;=\; \pmatrix{1 & 0 \cr 0 & -1}
$$
Two matrices $A$ and $B$ produce the same $x$ if and only if
$$
x_o \;=\; (B^{-1}A)x_o(B^{-1}A)^{-1}
$$
That is, $B^{-1}A$ is in the normalizer of $x_o$. We can check that this normalizer is a subgroup of the two-by-two invertible matrices. For $g=\pmatrix{a & b\cr c & d}$, the condition $gx_og^{-1}=x_o$ (multiplied out) is equivalent to $b=c=0$, if $-1\not=+1$, which does hold for $p\not=2$. That is, for $p\not=2$, the normalizer consists of diagonal matrices.
Thus, the collection of matrices $x$ with eigenvalues $1$ and $-1$ is in bijection with the coset space of two-by-two invertible matrices by the subgroup of diagonal invertible matrices.
The number of diagonal matrices is $(q-1)^2$, while the cardinality of two-by-two invertible matrices is $(q^2-1)(q^2-q)$. Thus, the number of such matrices $x$ is the quotient, namely, $(q+1)q$.
Thus, adding the identity and negative identity, we get $(q+1)q+1+1=q^2+q+2$. (Matching Alan Abrahams more concrete computation.)