Problem B. Counting Permutations
时间限制 1000 ms
内存限制 32 MB
When Tonyfang was studying monotonous queues, he came across the following problem:
For a permutation of length n $a_1,a_2...a_n$, define $l_i$ as maximum x satisfying $x<i$ and $a_x>a_i$, or 0 if such x not exists, $r_i$ as minimum x satisfying $x>i$ and $a_x > a_i$, or n+1 if not exists. Output $\sum_{i=1}^n \min(i-l_i,r_i-i)$.
Obviously, this problem is too easy for Tonyfang. So he thought about a harder version:
Given two integers n and x, counting the number of permutations of 1 to n which $\sum_{i=1}^n \min(i-l_i,r_i-i)=x$ where l and r are defined as above, output the number mod P.
Tonyfang solved it quickly, now comes your turn!
输入数据
输出数据
For every test case, output the number of valid permutations modulo P.
样例输入
样例输出
$ Mathjax font initiator $