$158^{158} \pmod {31}$ Trying to brush up on my modular arithmetic, and came across this problem.
Calculate $158^{158} \pmod {31}$.
Using the rule $a^b \pmod c = (a\pmod c)^b \pmod c$ I was able to reduce it to $3^{158} \pmod{31}$, which should be the same.
But from here, I'm stumped.
Which rule can I use to progress here? Or is there some immediately obvious solution already?
Thanks in advance for any help!
 A: Since $31$ is prime, you have that $a^{30} \equiv 1 \textrm{ (mod } 31)$ for $a \not \equiv 0 \textrm{ (mod } 31)$. Here of course we take $a = 3$. Now observe that $3^{158} = 3^{5 \cdot 30 + 8} = (3^{30})^5 \cdot 3^8$, and now taking the modulus we find that $3^{158} \equiv 1 \cdot 3^8 \textrm{ (mod } 31) \equiv 3^8 \textrm{ (mod } 31)$. Now $3^8 \textrm{ (mod } 31)$ isn't a hard calculation and results in $20 \textrm{ (mod } 31)$, and thus we find that 
$$ 158^{158} \equiv 20 \textrm{ (mod } 31)$$
A: Since $158\equiv3\pmod{31}$, you have to compute $3^{158}\pmod{31}$. You know (Fermat's little theorem) that $3^{30}\equiv1\pmod{31}$. Since $158\equiv8\pmod{30}$, you have to compute $3^8\pmod{31}$. But $3^4=81\equiv19\equiv-12\pmod{31}$, and therefore $3^8\equiv144\equiv20\pmod{31}$.
A: By FLT:
$$158^{30}\equiv 1\pmod {31}$$
thus:
$$158^{158}\equiv 158^{8}\equiv3^8\equiv9(-4)^2\equiv20\pmod {31}$$
A: Fermat's Little Theorem (extension):
$$3^{31}=3\pmod{31}\stackrel{\text{arit. mod}\,31}\implies 3^{158}=\left(3^{31}\right)^5\cdot 3^3=3^8=\left(3^4\right)^2=(-12)^2=20$$
