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 

<image>

for some function <image>. (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 109 + 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 109 + 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: 

  1. f(0) = 0, f(1) = 1, f(2) = 2. 
  2. f(0) = 0, f(1) = 2, f(2) = 1. 
  3. f(0) = f(1) = f(2) = 0.

Difficulty: MEDIUM_HARD
Tags: Graph traversal

Combinatorics

Number theory

Mathematics

Spanning trees

Starter code:

Solutions:
MOD = 10 ** 9 + 7

def f(a, b):
	if b == 1:
		return a % MOD
	elif b % 2 == 0:
		return f(a * a % MOD, b // 2)
	else:
		return a * f(a * a % MOD, b // 2) % MOD
(p, k) = map(int, input().split())
if k == 0:
	print(f(p, p - 1))
	exit()
if k == 1:
	print(f(p, p))
	exit()
t = 1
a = k
while a != 1:
	a = a * k % p
	t += 1
n = (p - 1) // t
print(f(p, n))


M = 10 ** 9 + 7

def period(p, k):
	c = 1
	ek = k
	while ek != 1:
		ek = ek * k % p
		c += 1
	return c

def functions(p, k):
	if k == 0:
		return pow(p, p - 1, M)
	elif k == 1:
		return pow(p, p, M)
	else:
		c = (p - 1) // period(p, k)
		return pow(p, c, M)
(p, k) = map(int, input().split())
print(functions(p, k))


c = 1000000007
(p, k) = map(int, input().split(' '))
if k == 0:
	answer = pow(p, p - 1, c)
elif k == 1:
	answer = pow(p, p, c)
else:
	size = 1
	new = k
	while new != 1:
		size += 1
		new *= k
		new = new % p
	cycles = (p - 1) // size
	answer = pow(p, cycles, c)
print(answer)


def m_pow(x, y, m):
	if y == 0:
		return 1
	if y & 1:
		return m_pow(x, y - 1, m) * x % m
	else:
		t = m_pow(x, y >> 1, m)
		return t * t % m
(p, k) = map(int, input().split())
used = [0] * p
if k == 0:
	print(m_pow(p, p - 1, 1000000007))
else:
	c = 1 if k == 1 else 0
	for x in range(1, p):
		if not used[x]:
			y = x
			while not used[y]:
				used[y] = True
				y = k * y % p
			c += 1
	print(m_pow(p, c, 1000000007))


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


import sys
T = 1
M = 1000000007

def process():
	(P, K) = list(map(int, input().split()))
	k = [K * x % P for x in range(P)]
	if not K:
		print(pow(P, P - 1, M))
		return
	f = [0] * P
	c = [0] * P
	ans = 1
	for i in range(0, P):
		if f[i]:
			continue
		cnt = 1
		u = i
		f[u] = 1
		while not f[k[u]]:
			u = k[u]
			f[u] = 1
			cnt = cnt + 1
		c[cnt] = c[cnt] + 1
	for i in range(1, P):
		if c[i] != 0:
			cnt = i * c[i] + (1 if i > 1 else 0)
			ans = ans * pow(cnt, c[i], M) % M
	print(ans)
for _ in range(T):
	process()


def main():
	t = 1
	a = k
	if k == 0:
		return n - 1
	if k == 1:
		return n
	while a != 1:
		a = a * k % n
		t += 1
		p = (n - 1) // t
	return p
(n, k) = map(int, input().split())
mod = 10 ** 9 + 7
p = main()
print(pow(n, p, mod))


import math

def expmod(base, expon, mod):
	ans = 1
	for i in range(1, expon + 1):
		ans = ans * base % mod
	return ans
(p, k) = input().split()
s = 10 ** 9 + 7
k = int(k)
p = int(p)
ord = 1
done = 0
if k == 0:
	z = p - 1
if k == 1:
	z = p
else:
	for i in range(2, p + 1):
		if done == 0:
			if (p - 1) % i == 0:
				if expmod(k, i, p) == 1:
					ord = i
					done = 1
	z = int((p - 1) / ord)
rem = expmod(p, z, s)
print(int(rem))


(p, k) = map(int, input().split())
res = set()
ex = p - 1
if k != 0:
	if k == 1:
		ex = p
	else:
		c = k
		for i in range(p):
			res.add(c)
			c *= k
			c %= p
		ex = (p - 1) // len(res)
ans = 1
for i in range(ex):
	ans *= p
	ans %= 1000000007
print(ans)


(p, k) = map(int, input().split())
m = 10 ** 9 + 7
if k == 0:
	print(pow(p, p - 1, m))
elif k == 1:
	print(pow(p, p, m))
else:
	used = [0] * p
	res = 0
	for i in range(1, p):
		a = i
		if used[a] == 0:
			res += 1
		while used[a] == 0:
			used[a] = 1
			a = k * a % p
	print(pow(p, res, m))