The problem: Find $3^{31^{2023}} \mod 23$
My attempt:
From Fermat's little theorem, we have $$31^{22}\equiv 1 \Rightarrow (31^{22})^{92} \equiv 1 \Rightarrow 31^{2024} \equiv 1 \mod 23$$ Motivated by this we first determine $31^{2023} \mod 23$. We let $a \equiv 31^{2023} \mod 23$. It follows that:
\begin{align*} a &\equiv 31^{2023} \\ 31 \cdot a &\equiv 31^{2024} \\ 8 \cdot a &\equiv 1 \\ 24 \cdot a &\equiv 3 \\ a & \equiv 3 \end{align*}
Then we have:
$$3^{31^{2023}} \equiv 3^3 \equiv 4 \mod 23$$
So we have $3^{31^{2023}} \mod 23 \equiv 4$
Error: Although I got the right answer, I was told that although $31^{2023} \equiv 3 \mod 23$ is correct, it does not follow that $3^{31^{2023}} \equiv 3^3 \mod 23$ and by Euler's Theorem, I have to calculate $31^{2023} \mod 22$. I'm just not sure how and in what situation I need to apply this to, although I did find that $31^{2023} \equiv 3 \mod 22$.
Can someone help explain Euler's theorem again and how to apply it in general for nested exponents?
P.S. I'm not really sure what tags to use for this so feel free to change it if my tags aren't right.