As behooves any intelligent schoolboy, Kevin Sun is studying psycowlogy, cowculus, and cryptcowgraphy at the Bovinia State University (BGU) under Farmer Ivan. During his Mathematics of Olympiads (MoO) class, Kevin was confronted with a weird functional equation and needs your help. For two fixed integers k and p, where p is an odd prime number, the functional equation states that $f(k x \operatorname{mod} p) \equiv k \cdot f(x) \operatorname{mod} p$ 

for some function $f : \{0,1,2, \cdots, p - 1 \} \rightarrow \{0,1,2, \cdots, p - 1 \}$. (This equation should hold for any integer x in the range 0 to p - 1, inclusive.)

It turns out that f can actually be many different functions. Instead of finding a solution, Kevin wants you to count the number of distinct functions f that satisfy this equation. Since the answer may be very large, you should print your result modulo 10^9 + 7.


-----Input-----

The input consists of two space-separated integers p and k (3 ≤ p ≤ 1 000 000, 0 ≤ k ≤ p - 1) on a single line. It is guaranteed that p is an odd prime number.


-----Output-----

Print a single integer, the number of distinct functions f modulo 10^9 + 7.


-----Examples-----
Input
3 2

Output
3

Input
5 4

Output
25



-----Note-----

In the first sample, p = 3 and k = 2. The following functions work:   f(0) = 0, f(1) = 1, f(2) = 2.  f(0) = 0, f(1) = 2, f(2) = 1.  f(0) = f(1) = f(2) = 0.

Difficulty: competition

Starter code:

Solutions:
def main():
	p, k = list(map(int, input().split()))
	s = 1
	m = pow(10,9)+7
	if k == 0:
		s = pow(p,p-1,m)
	elif k == 1:
		s = pow(p,p,m)
	else:
		o = 1
		n = k
		while n != 1:
			n = k*n %p
			o += 1
		c = (p-1)//o
		s = pow(p,c,m)
	print(s%m)
main()


MOD=int(1e9+7)
n,k=list(map(int,input().split()))
if k<2:p=n-(1-k)
else:
	t=1
	a=k
	while a!=1:
		a=a*k%n
		t+=1
	p=(n-1)//t
print(pow(n,p,MOD))


q = 10 ** 9 + 7

def solve(p, k):
    if k == 0:
        return pow(p, p-1, q)
    if k == 1:
        return pow(p, p, q)
    x, y = k, 1
    while x != 1:
        x *= k
        x %= p
        y += 1

    return pow(p, (p-1)//y, q)

p, k = list(map(int, input().split()))
print(solve(p, k))


3

def is_good(f, p, k):
    for i in range(p):
        if f[(k * i) % p] != (k * f[i]) % p:
            return False
    return True


def gen(f, p, k, i):
    if i == p:
        return is_good(f, p, k)
    else:
        ans = 0
        for j in range(p):
            f.append(j)
            ans += gen(f, p, k, i + 1)
            f.pop()
        return ans


def solve(p, k):
    num = 0
    used = set()
    for j in range(p):
        if j not in used:
            num += 1
        i = j
        while i not in used:
            used.add(i)
            i = (i * k) % p
    return pow(p, num - (k != 1), 10 ** 9 + 7)
            
    
    
p, k = list(map(int, input().split()))
#print(gen([], p, k, 0))
print(solve(p, k))


MOD=int(1e9+7)
n,k=list(map(int,input().split()))
if k<2:p=n-(1-k)
else:
	t=1
	a=k
	while a!=1:
		a=a*k%n
		t+=1
	p=(n-1)//t
print(pow(n,p,MOD))



MOD=int(1e9+7)
n,k=list(map(int,input().split()))
if k<2:p=n-(1-k)
else:
	t=1
	a=k
	while a!=1:
		a=a*k%n
		t+=1
	p=(n-1)//t
print(pow(n,p,MOD))



MOD=int(1e9+7)
n,k=list(map(int,input().split()))
if k<2:p=n-(1-k)
else:
	t=1
	a=k
	while a!=1:
		a=a*k%n
		t+=1
	p=(n-1)//t
print(pow(n,p,MOD))



MOD=int(1e9+7)
n,k=list(map(int,input().split()))
if k<2:p=n-(1-k)
else:
	t=1
	a=k
	while a!=1:
		a=a*k%n
		t+=1
	p=(n-1)//t
print(pow(n,p,MOD))



MOD=int(1e9+7)
n,k=list(map(int,input().split()))
if k<2:p=n-(1-k)
else:
	t=1
	a=k
	while a!=1:
		a=a*k%n
		t+=1
	p=(n-1)//t
print(pow(n,p,MOD))



MOD=int(1e9+7)
n,k=list(map(int,input().split()))
if k<2:p=n-(1-k)
else:
	t=1
	a=k
	while a!=1:
		a=a*k%n
		t+=1
	p=(n-1)//t
print(pow(n,p,MOD))



MOD=int(1e9+7)
n,k=list(map(int,input().split()))
if k<2:p=n-(1-k)
else:
	t=1
	a=k
	while a!=1:
		a=a*k%n
		t+=1
	p=(n-1)//t
print(pow(n,p,MOD))



MOD=int(1e9+7)
n,k=list(map(int,input().split()))
if k<2:p=n-(1-k)
else:
	t=1
	a=k
	while a!=1:
		a=a*k%n
		t+=1
	p=(n-1)//t
print(pow(n,p,MOD))



def main():
	p, k = list(map(int, input().split()))
	s = 1
	m = pow(10,9)+7
	if k == 0:
		s = pow(p,p-1,m)
	elif k == 1:
		s = pow(p,p,m)
	else:
		o = 1
		n = k
		while n != 1:
			n = k*n %p
			o += 1
		c = (p-1)//o
		s = pow(p,c,m)
	print(s%m)
main()




# Made By Mostafa_Khaled


from sys import stdin
import sys
sys.setrecursionlimit(10**6)

n,k=list(map(int,stdin.readline().strip().split()))
def dfs( n):
    visited[n]=True
    while not visited[adj[n]]:
        n=adj[n]
        visited[n]=True
mod=10**9+7
adj=[-1 for i in range(n+1)]
visited=[False for i in range(n+1)]
for i in range(n):
	adj[i]=(i*k)%n
pairs=0
for i in range(1,n):
    if not visited[i]:
        dfs(i)
        pairs+=1
    
if k==1:
    print(pow(n,n,mod))
else:
    print(pow(n,pairs,mod))


MOD=int(1e9+7)
n,k=map(int,input().split())
if k<2:p=n-(1-k)
else:
	t=1
	a=k
	while a!=1:
		a=a*k%n
		t+=1
	p=(n-1)//t
print(pow(n,p,MOD))