<s>[INST] Remainder of $7^{7^{7}}$ divided by $32$ The problem is to find remainder of $7^{7^{7}}$ divided by $32$ the quickest way.  
My attempt:
$32 = 8.4$
Mod 8: $7^{7^{7}}\equiv(-1)^{7^{7}}\equiv-1$
Mod 4: $7^{7^{7}}\equiv(-1)^{7^{7}}\equiv-1$
Hence in mod $32$: $7^{7^{7}}\equiv(-1)$ mod $8.(-1)$mod$ 4 (?) \equiv 7.3\equiv21 $ 
I am quite worried about the last step. I can't justify it.
 Is there a theorem related to (?) or
Is there a better and correct way to do this ?  
 [/INST] Fact: $a^{2^{k-2}}\equiv 1\pmod{2^k}$ for all $k\ge 3$, odd $a$.
Proof: We use induction. $a^2\equiv 1\pmod{8}$ for all odd $a$.
If $a^{2^{m-2}}\equiv 1\pmod{2^m}$, we want to prove $a^{2^{m-1}}\equiv 1\pmod{2^{m+1}}$.
I.e. if $a^{2^{m-2}}=2^mk+1$, we want to prove $\left(2^mk+1\right)^2\equiv 1\pmod{2^{m+1}}$.
$$\left(2^mk+1\right)^2\equiv 2^{2m}k+2^{m+1}k+1\equiv 1\pmod{2^{m+1}}$$
So $7^{8}\equiv 1\pmod{32}$. Then $$7^{7^7}\equiv 7^{7^7\pmod{8}}\equiv 7^{(-1)^7}\equiv 7^{-1}\pmod{32}$$
$$\equiv \frac{1}{7}\equiv \frac{5}{7\cdot 5}\equiv \frac{5}{3}\equiv \frac{-27}{3}\equiv -9\equiv 23\pmod{32}$$
</s>