This page is still under construction.

Parts of this page are still being built. What you see may change.

Great Pow!

Time limit10sMemory limit64 MB

Summary
Compute the tower of k+1 copies of a modulo a+1, where a is minus one so only the parity of the upper tower matters.
Level

Medium5 of 10

Topics
Number theory, Math
Solved
No attempts yet

Problem

Write the power aba^b as powa(b)\mathrm{pow}_a(b).

Then define powa0(a)=a\mathrm{pow}_a^0(a) = a and powak+1(a)=powa(powak(a))\mathrm{pow}_a^{k+1}(a) = \mathrm{pow}_a\bigl(\mathrm{pow}_a^k(a)\bigr) for k≥0k \ge 0.

Given aa and kk, compute powak(a)\mathrm{pow}_a^k(a), the value of the tower built from k+1k+1 copies of aa:

aaa⋯aa^{a^{a^{\cdots^{a}}}}

The tower is evaluated from the top down. For k=2k = 2, note that (aa)a≠a(aa)(a^a)^a \neq a^{(a^a)}, and the value you need is the latter.

Input

The first line contains aa and kk separated by a space. (1≤a≤1091 \le a \le 10^9, 0≤k≤1090 \le k \le 10^9)

Output

Print powak(a)\mathrm{pow}_a^k(a) modulo a+1a+1. The value itself can be enormous, so only the remainder is printed.

Hint

pow23(2)=2222=65536\mathrm{pow}_2^3(2) = 2^{2^{2^{2}}} = 65536, so print 65536 modulo 3, which is 1.

Examples2

  1. Example 1

    Input
    2 3
    
    Expected output
    1
    
  2. Example 2

    Input
    2 0
    
    Expected output
    2