Compute modulo of prime power tower

I have a prime number $p$, and I need to compute $p$ ↑↑ $k$ mod $m$.

here p ↑↑ k can be written as p ^ (p ^ (p ^ (p ... k times)))

for example -

$p = 5, k = 3, m = 3$

5 ↑↑ 3 = 5 ^ (5 ^ 5) % 3 = 2


Here $p$ is prime number, $2 <= p <= 10 ^ 8$, and $p - 2 <= m <= 10 ^ 8$.

• So what exactly is your question? What kind of answer are you expecting here? – Omnomnomnom Apr 26 '17 at 16:18
• If $k$ is higher than a certain constant (different for every prime), the residue will be constant. That might be interesting to you. – Mastrem Apr 26 '17 at 16:19
• @Omnomnomnom i need ans for p ↑↑ k mod m – Atul Apr 26 '17 at 16:36
• This answer might help: stackoverflow.com/questions/21367824/… – Isaac Browne Apr 26 '17 at 17:05
• But the thing which I find weird is that the same question was posted here 4 hours ago and here 2 days ago.... – Blitzkrieg Apr 26 '17 at 17:47