Let $p$ be a prime number which is not equal to 2. Prove that if there exists an integer $a$ such that $a^2 \equiv -1 \text{(mod p) }$ then $p \equiv 1 \text{(mod 4)}$.
My attempt: let $p$ be a prime number not equal to 2. Suppose $p \not \equiv 1 \text{(mod 4)}$. Since $p \not= 2$, p must be odd, therefore $p \equiv 3 \text{(mod 4)}$
We have that $a^2 \equiv -1 \text{(mod p) }$ so $p| a^2 + 1 \implies a^2 + 1 = pm$ for some integer $n$.
$a^2 \equiv 0 \text{(mod 4)} \text{ or } a^2 \equiv 1 \text{(mod 4)} \implies a^2 + 1 \equiv 1 \text{(mod 4)} \text{ or } a^2 + 1 \equiv 2 \text{(mod 4)}$. This means that
$a^2 + 1 \equiv pm \equiv 3m \equiv 1 \text{(mod 4)}$ or
$a^2 + 1 \equiv pn \equiv 3n \equiv 2 \text{(mod 4)}$
But I am not able to reach any contradiction. What am I missing here?