PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#DSU0000047. Số học môđun kỳ lạ (Moodular Arithmetic)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Số học môđun kỳ lạ (Moodular Arithmetic)

Moodular Arithmetic

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

You are given an odd prime pp and an integer kk satisfying 0≤k<p0\le k<p. Consider the residue set S={0,1,…,p−1}S=\{0,1,\ldots,p-1\}.

Count the functions f:S→Sf:S\to S such that the following equality holds for every x∈Sx\in S:

f((k⋅x) mod p)=(k⋅f(x)) mod p.f((k\cdot x)\bmod p)=(k\cdot f(x))\bmod p.

A function is determined by all pp values f(0),f(1),…,f(p−1)f(0),f(1),\ldots,f(p-1). Two functions are different if their outputs differ for at least one argument. The function is not required to be injective, surjective, or a permutation. Because the count may be very large, output it modulo 109+710^9+7.

Input

A single line contains p,kp,k; pp is an odd prime.

Output

Print the number of valid functions modulo 109+710^9+7.

Subtasks

  • Subtask 1 (20 points): 3≤p≤1013\le p\le101; pp is an odd prime; 0≤k<p0\le k<p.
  • Subtask 2 (30 points): 3≤p≤100073\le p\le10007; pp is an odd prime; 0≤k<p0\le k<p.
  • Subtask 3 (50 points): 3≤p≤1063\le p\le10^6; pp is an odd prime; 0≤k<p0\le k<p.

Examples

Input

5 4

Output

25

Explanation

There are two independent cycles among the nonzero residues; each provides five choices.