Write the power ab as powa(b).
Then define powa0(a)=a and powak+1(a)=powa(powak(a)) for k≥0.
Given a and k, compute powak(a), the value of the tower built from k+1 copies of a:
aaa⋯a
The tower is evaluated from the top down. For k=2, note that (aa)a=a(aa), and the value you need is the latter.
The first line contains a and k separated by a space. (1≤a≤109, 0≤k≤109)
Print powak(a) modulo a+1. The value itself can be enormous, so only the remainder is printed.
pow23(2)=2222=65536, so print 65536 modulo 3, which is 1.