def egcd(a, b): if a == 0: return b, 0, 1 else: g, y, x = egcd(b % a, a) return g, x - (b // a) * y, y mod=10**9+7 x,k=map(int,input().split()) gcd,a,b=egcd(mod-1,-k) b*=-1 b%=mod-1 n=pow(x,b,mod) print(n)