CF BUDDY
← Problems·

603B · Moodular Arithmetic

1800 · combinatorics, dfs and similar, dsu

Problem: 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(kxmodp)kf(x)modpf(kx \mod p) \equiv k \cdot f(x) \mod p

for some function f:{0,1,2,,p1}{0,1,2,,p1}f: \{0, 1, 2, \cdots, p-1\} \to \{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 109 + 7.

Input Format: 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 Format: Print a single integer, the number of distinct functions f modulo 109 + 7.

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.

Sample Cases

Case 1

Input

3 2

Output

3

Case 2

Input

5 4

Output

25

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback