Given a function $f(x,n) = x^{x^{x^{x}}}$ where $n = \text{how many powers of }x$, how to find value of $f(x,n) \bmod k$, where $k < x$ and $k$ can be prime or not prime and has large values.
For example: $f(5,2) \bmod 3 = 5^5 \bmod 3 = 3125 \bmod 3 = 2.$