Yet Yet Another Permutation Problem
Contest: Codeforces Round 930 (Div. 1)
Difficulty rating: 3400
Tags: divide and conquer, fft, math

You are given a permutation $$$p$$$ of length $$$n$$$.

Please count the number of permutations $$$q$$$ of length $$$n$$$ which satisfy the following:

- for each $$$1 \le i < n$$$, $$$\max(q_1,\ldots,q_i) \neq \max(p_1,\ldots,p_i)$$$.

Since the answer may be large, output the answer modulo $$$998\,244\,353$$$.

Input format:
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows.

The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$).

The second line of each test case contains $$$n$$$ integers $$$p_1, p_2, \ldots, p_n$$$ ($$$1 \le p_i \le n$$$). It is guaranteed that $$$p$$$ is a permutation.

It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

Output format:
For each test case, print a single integer — the answer modulo $$$998\,244\,353$$$.

Constraints:
Time limit: 5.0 s
Memory limit: 1024.0 MB

Note:
In the first test case, $$$p = [2, 1]$$$. The only suitable $$$q$$$ is $$$[1, 2]$$$. Indeed, we need to satisfy the inequality $$$q_1 \neq p_1$$$. It only holds for $$$q = [1, 2]$$$.

In the second test case, $$$p = [1, 2, 3]$$$. So $$$q$$$ has to satisfy two inequalities: $$$q_1 \neq p_1$$$ and $$$\max(q_1, q_2) \neq \max(1, 2) = 2$$$. One can prove that this only holds for the following $$$3$$$ permutations:

- $$$q = [2, 3, 1]$$$: in this case $$$q_1 = 2 \neq 1$$$ and $$$\max(q_1, q_2) = 3 \neq 2$$$;
- $$$q = [3, 1, 2]$$$: in this case $$$q_1 = 3 \neq 1$$$ and $$$\max(q_1, q_2) = 3 \neq 2$$$;
- $$$q = [3, 2, 1]$$$: in this case $$$q_1 = 3 \neq 1$$$ and $$$\max(q_1, q_2) = 3 \neq 2$$$.

Example input:
6
2
2 1
3
1 2 3
3
3 1 2
4
2 4 1 3
5
3 5 1 4 2
15
6 13 2 8 7 11 1 3 9 15 4 5 12 10 14

Example output:
1
3
2
4
18
424488915

Editorial:
We found it difficult to directly calculate all valid permutations. Consider calculating all invalid permutations and subtract it from $$$n!$$$.
Let's first make some notes. We note $$$P_i=max(p_1,...,p_i),Q_i=max(q_1,...,q_i)$$$,and $$$last_i=max(j)(P_j \neq P_i)$$$.And note the values of all extreme points is $$$val[1,...,m]$$$ and positions is $$$pos[1,...,m]$$$.
For example,$$$p=[3,1,4,2,6,5]$$$,we get $$$m=3$$$, $$$val=[3,4,6]$$$ and $$$pos=[1,3,5]$$$.
Let's call a permutation $$$q$$$ "i-invalid" if there's an index $$$j$$$ satisfying $$$Q_j=P_j=val_i$$$.
Note $$$S_i$$$ as the set of all "i-invalid" permutations.According to the Inclusion-Exclusion Principle,the answer is $$$n!-(|S_1|+|S_2|+...)+(|S_1∩S_2|+|S_1∩S_3|+...)-...$$$
Consider put $$$val_i$$$ in $$$q$$$ to make $$$q$$$ "i-invalid".There're $$$2$$$ kinds of ways:
• $$$q_j=val_i(1 \le j < pos_i)$$$ and $$$Q_{pos_i}=val_i$$$;
• $$$q_j=val_i(pos_i \le j< pos_{i+1})$$$ and $$$Q_{pos_i}=val_i$$$.
In both cases,we call the first $$$max(j,pos_i)$$$ numbers are "determined". In other words,they're some numbers in $$$[1,val_i]$$$.
Then we can find an $$$O(n^2)$$$ DP. Note $$$dp_{i,j}$$$ — $$$i$$$ stands for the first $$$i$$$ numbers is "determined", and $$$j$$$ stands for $$$j$$$ extreme values are "invalid". In fact we only care about the parity of $$$j$$$,so $$$j=0,1$$$. Note for the first $$$i$$$ numbers are "determined" automatically means $$$Q_i=P_i$$$. In addition,$$$q_i=P_i(P_i=P_{i-1})$$$ or $$$q_i<P_i(P_i \neq P_{i-1})$$$.
We get a DP formula $$$dp_{i,j}= \Sigma_{k=0}^{last_i} dp_{k,j \oplus 1} \cdot A^{P_i-k-1}_{i-k-1} \cdot (P_i==P_{i+1}?1:i-k)$$$
Here is a small trick to reduce the constant. If we note $$$f_i=dp_{i,0}-dp_{i,1}$$$, we get a more concise formula $$$f_{i}= -\Sigma_{j=0}^{last_i} f_{j} \cdot A^{P_i-j-1}_{i-j-1} \cdot (P_i==P_{i+1}?1:i-j)$$$
We perform the following transformation on the formula.
① For $$$i$$$ satisfying $$$P_i=P_{i-1}$$$, we get
$$$f_{i}= -\Sigma_{j=0}^{last_i} f_{j} \cdot A^{P_i-j-1}_{i-j-1} = -(P_i-i)^{-1} \Sigma_{j=0}^{last_i} f_{j} \cdot (P_i-j-1)$$$
② For $$$i$$$ satisfying $$$P_i \neq P_{i-1}$$$, we get
$$$f_{i}= -\Sigma_{j=0}^{last_i} f_{j} \cdot A^{P_i-j-1}_{i-j-1} \cdot (i-j) = -i(P_i-i)^{-1} \Sigma_{j=0}^{last_i} f_{j} \cdot (P_i-j-1) + (P_i-i)^{-1} \Sigma_{j=0}^{last_i} jf_{j} \cdot (P_i-j-1)$$$
After that, we have transformed the formula into a classical form $$$f(i)= h(i) \Sigma_{j=0}^{i-1} f(j) \cdot g(i-j)$$$, which can be calculate in $$$O(n log^2 n)$$$ by D&C+FFT.