Count the number of n×n matrices A satisfying the following condition modulo m.
-
Ai,j∈{0,1,2} for all 1≤i,j≤n.
-
Ai,j=Aj,i for all 1≤i,j≤n.
-
Ai,1+Ai,2+⋯+Ai,n=2 for all 1≤i≤n.
-
A1,1=A2,2=⋯=An,n=0.
Input
The input consists of several test cases and is terminated by end-of-file.
Each test case contains two integers n and m.
- 1≤n≤105
- 1≤m≤109
- The sum of n does not exceed 107.
Output
For each test case, print an integer which denotes the result.
Example
Input
3 1000000000
100000 1000000000
Output
1
507109376
大佬的解释
邻接矩阵:dp[i][j]=w :i点与j点之间的权值为w
无向图的邻接矩阵:满足 dp[i][j]=dp[j][i]
将要求的矩阵当成无向图的邻接矩阵,权值当成连接的边数
对角线为0,即自己与自己不成环
每行和为2,可理解成要嘛就是自己与一个点之间连了两条边,要么就是自己连了两个不同的点
所有点的度数都为2,且存在重边但不存在自环。这种情况的图为多个环,即每个点都在且仅在一个环里。因为一旦不成环,则定有一个点只有一个度,与条件矛盾。
dp[n]为第n个点的成环方案
情况1即自环:从n-1个点中取一个点出来与第n个点组成一个新环,则(n-1)*dp[n-2]种方案
情况2:从n-1个点种取出x个点,剩下的点与第n个点组成一个新环,(2<=x<=n−3),即sum(C(n-1,k)