#abc284g. G - Only Once

G - Only Once

Score : 600600 points

问题描述

对于一个长度为 NN 的序列 A=(A1,A2,,AN)A = (A_1, A_2, \dots, A_N),其中包含从 1 到 NN(包括两端)之间的整数,以及一个整数 i (1iN)i\ (1\leq i \leq N),让我们定义一个长度为 1010010^{100} 的序列 Bi=(Bi,1,Bi,2,,Bi,10100)B_i=(B_{i,1}, B_{i,2}, \dots, B_{i,10^{100}}) 如下:

  • 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})

另外,我们定义 SiS_i 为在序列 BiB_i 中恰好出现一次的不同整数的数量。更正式地说,SiS_i 是满足存在唯一一个索引 j (1j10100)j\ (1\leq j\leq 10^{100}) 满足 Bi,j=kB_{i,j}=k 的值 kk 的数量。

已知一个整数 NN。有 NNN^N 种可能的序列 AA。求所有这些序列中 i=1NSi\displaystyle \sum_{i=1}^{N} S_i 的和,结果对 MM 取模。

以上为通义千问 qwen-max 翻译,仅供参考。

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.

Sample Input 1

4 100000000

Sample Output 1

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.

Sample Input 2

7 1000000000

Sample Output 2

5817084

Sample Input 3

2023 998244353

Sample Output 3

737481389

Print the sum modulo MM.

Sample Input 4

100000 353442899

Sample Output 4

271798911

update @ 2024/3/10 11:54:34