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

#GL0000256. Cặp lũy thừa (Power Pair)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Cặp lũy thừa (Power Pair)

Power Pair

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

You are given a prime number PP. Consider ordered pairs of integers (x,y)(x,y) such that both xx and yy are between 00 and P−1P-1, inclusive.

A pair is valid if there exists at least one positive integer nn such that the remainder of xnx^n upon division by PP is yy, equivalently

xn≡y(modP).x^n\equiv y\pmod P.

Even if several positive exponents satisfy the condition, count that ordered pair only once. The pairs (x,y)(x,y) and (y,x)(y,x) are distinct whenever x≠yx\ne y.

Count the valid ordered pairs and output the count modulo 998244353998244353.

Input

The only line contains the prime number PP.

Output

Print one integer: the number of valid ordered pairs (x,y)(x,y), modulo 998244353998244353.

Subtasks

  • Subtask 1 (20 points): 2≤P≤1002\le P\le100; PP is prime.
  • Subtask 2 (30 points): 2≤P≤1062\le P\le10^6; PP is prime.
  • Subtask 3 (50 points): 2≤P≤10122\le P\le10^{12}; PP is prime.

Examples

Example 1

Input

3

Output

4

Explanation

For P=3P=3, the possible values of xx are 00, 11, and 22. For x=0x=0, all positive powers are 00, so only y=0y=0 works. For x=1x=1, only y=1y=1 works. For x=2x=2, the remainders of consecutive powers are 2,1,2,1,…2,1,2,1,\ldots, so either y=1y=1 or y=2y=2 works. The four valid pairs are (0,0)(0,0), (1,1)(1,1), (2,1)(2,1), and (2,2)(2,2).

Example 2

Input

11

Output

64

Explanation

For P=11P=11, consider x=0,1,2,…,10x=0,1,2,\ldots,10. The numbers of reachable remainders yy from positive powers of each xx are 1,1,10,5,5,5,10,10,10,5,21,1,10,5,5,5,10,10,10,5,2, respectively. For example, the positive powers of x=3x=3 yield the repeating sequence 3,9,5,4,13,9,5,4,1, giving five possible values of yy. Therefore the total count is 1+1+10+5+5+5+10+10+10+5+2=641+1+10+5+5+5+10+10+10+5+2=64.

Example 3

Input

998244353

Output

329133417

Explanation

Here P=998244353P=998244353 is large, so enumerating ordered pairs is impractical. We have P−1=998244352=223⋅7⋅17P-1=998244352=2^{23}\cdot7\cdot17. Grouping nonzero values by the number of distinct remainders attainable by positive powers, and adding the pair (0,0)(0,0), yields 329133417329133417 modulo 998244353998244353.