#P10004. [集训队互测 2023] Permutation Counting 2

[集训队互测 2023] Permutation Counting 2

Problem Description

Given nn, for each pair x,y[0,n)x,y\in [0,n), find how many permutations pp of 1n1\sim n satisfy the following conditions:

  • i=1n1[pi<pi+1]=x\sum\limits_{i=1}^{n-1}[p_i<p_{i+1}]=x.

  • i=1n1[pi1<pi+11]=y\sum\limits_{i=1}^{n-1}[p^{-1}_i<p^{-1}_{i+1}]=y.

Here p1p^{-1} denotes the inverse permutation of pp, satisfying ppi1=ip^{-1}_{p_i}=i.

Output the answer modulo the given prime MODMOD.

Input Format

One line with two integers, n,MODn,MOD.

Output Format

Output nn lines, each containing nn integers. The number in row ii and column jj denotes the answer when x=i1,y=j1x=i-1,y=j-1.

3 1000000007
1 0 0
0 4 0
0 0 1
5 1000000007
1 0 0 0 0
0 20 6 0 0
0 6 54 6 0
0 0 6 20 0
0 0 0 0 1
10 1000000007
1 0 0 0 0 0 0 0 0 0
0 165 462 330 55 1 0 0 0 0
0 462 9273 22023 13750 2266 66 0 0 0
0 330 22023 147301 203610 75306 6556 66 0 0 
0 55 13750 203610 592130 423236 75306 2266 1 0
0 1 2266 75306 423236 592130 203610 13750 55 0
0 0 66 6556 75306 203610 147301 22023 330 0
0 0 0 66 2266 13750 22023 9273 462 0
0 0 0 0 1 55 330 462 165 0
0 0 0 0 0 0 0 0 0 1

Hint

For 100%100\% of the testdata, 1n5001\le n\le 500, 109MOD1.01×10910^9\le MOD\le 1.01\times 10^9, and MODMOD is guaranteed to be prime.

Subtask1(10%):n8\operatorname{Subtask} 1(10\%):n\le 8.

Subtask2(15%):n16\operatorname{Subtask} 2(15\%):n\le 16.

Subtask3(25%):n40\operatorname{Subtask} 3(25\%):n\le 40.

Subtask4(25%):n100\operatorname{Subtask} 4(25\%):n\le 100.

Subtask5(25%):\operatorname{Subtask} 5(25\%): No special constraints.

Translated by ChatGPT 5