atcoder#ABC284G. [ABC284G] Only Once

[ABC284G] Only Once

Score : 600600 points

Problem Statement

For a sequence of length NN, A=(A1,A2,,AN)A = (A_1,A_2,\dots,A_N), consisting of integers between 11 and NN, inclusive, and an integer i (1iN)i\ (1\leq i \leq N), let us define a sequence of length 1010010^{100}, Bi=(Bi,1,Bi,2,,Bi,10100)B_i=(B_{i,1},B_{i,2},\dots,B_{i,10^{100}}), as follows.

  • Bi,1=iB_{i,1}=i.
  • Bi,j+1=ABi,j (1j<10100)B_{i,j+1}=A_{B_{i,j}}\ (1\leq j<10^{100}).

Additionally, let us define SiS_i as the number of distinct integers that occur exactly once in the sequence BiB_i. More formally, SiS_i is the number of values kk such that exactly one index j (1j10100)j\ (1\leq j\leq 10^{100}) satisfies Bi,j=kB_{i,j}=k.

You are given an integer NN. There are NNN^N sequences that can be AA. Find the sum of i=1NSi\displaystyle \sum_{i=1}^{N} S_i over all of them, modulo MM.

Constraints

  • 1N2×1051\leq N \leq 2\times 10^5
  • 108M10910^8\leq M \leq 10^9
  • NN and MM are integers.

Input

The input is given from Standard Input in the following format:

NN MM

Output

Print the answer as an integer.

4 100000000
624

As an example, let us consider the case A=(2,3,3,4)A=(2,3,3,4).

  • For i=1i=1: we have B1=(1,2,3,3,3,)B_1=(1,2,3,3,3,\dots), where two integers, 11 and 22, occur exactly once, so S1=2S_1=2.
  • For i=2i=2: we have B2=(2,3,3,3,)B_2=(2,3,3,3,\dots), where one integer, 22, occurs exactly once, so S2=1S_2=1.
  • For i=3i=3: we have B3=(3,3,3,)B_3=(3,3,3,\dots), where no integers occur exactly once, so S3=0S_3=0.
  • For i=4i=4: we have B4=(4,4,4,)B_4=(4,4,4,\dots), where no integers occur exactly once, so S4=0S_4=0.

Thus, we have i=1NSi=2+1+0+0=3\displaystyle \sum_{i=1}^{N} S_i=2+1+0+0=3.

If we similarly compute i=1NSi\displaystyle \sum_{i=1}^{N} S_i for the other 255255 sequences, the sum of this value over all 256256 sequences is 624624.

7 1000000000
5817084
2023 998244353
737481389

Print the sum modulo MM.

100000 353442899
271798911