#P62. [KBC005F] Count 3

[KBC005F] Count 3

Source

This problem is adapted from Long Long OJ. All rights reserved.

Problem Description

Given integers pp and kk, find the number of ways to construct a non-negative integer sequence ff of length pp (indexed from 00) such that for any integer xx between 00 and p1p-1, fkxmodp=kfxmodpf_{kx\bmod p}=kf_x\bmod p.

The answer should be taken modulo 109+710^9+7.

Input Format

A single line containing two integers, representing pp and kk respectively.

Output Format

A single line containing an integer, representing the number of valid schemes.

Samples

3 2
3
5 4
25

Sample 1 Explanation

The 3 valid sequences are as follows:

  1. f0=0,f1=1,f2=2f_0=0,f_1=1,f_2=2;
  2. f0=0,f1=2,f2=1f_0=0,f_1=2,f_2=1;
  3. f0=f1=f2=0f_0=f_1=f_2=0.

Data Range

  • For 10%10\% of the data: 3p73\le p \le 7.
  • For another 10%10\% of the data: k=0k=0.
  • For another 10%10\% of the data: k=1k=1.
  • For 100%100\% of the data: 3p1063\le p \le 10^6, 0kp10 \le k \le p-1, and pp is a prime number.