11453: ABC380 - G - Another Shuffle Window
[Creator : ]
Description
<p>You are given a permutation $P$ of $(1,2,\dots,N)$ and an integer $K$. </p>
<p>Find the expected value, modulo $998244353$, of the inversion number of $P$ after performing the following operation:</p>
<ul>
<li>First, choose an integer $i$ uniformly at random between $1$ and $N - K + 1$, inclusive.</li>
<li>Then, shuffle $P_i, P_{i+1}, \dots, P_{i+K-1}$ uniformly at random.</li>
</ul>
<details>
<summary>>>What is the inversion number?<<</summary>
The inversion number of a sequence $(A_1, A_2, \dots, A_N)$ is the number of integer pairs $(i, j)$ satisfying $1 \le i < j \le N$ and $A_i > A_j$.
</details>
<details>
<summary>>>What does "expected value modulo $998244353$" mean?<<</summary>
It can be proved that the sought expected value is always rational. Under the constraints of this problem, when this value is represented as an irreducible fraction $\frac{P}{Q}$, it can also be proved that $Q \not\equiv 0 \pmod{998244353}$. Thus, there is a unique integer $R$ satisfying $R \times Q \equiv P \pmod{998244353}, \ 0 \le R < 998244353$. Report this integer $R$.
</details>
<p>Find the expected value, modulo $998244353$, of the inversion number of $P$ after performing the following operation:</p>
<ul>
<li>First, choose an integer $i$ uniformly at random between $1$ and $N - K + 1$, inclusive.</li>
<li>Then, shuffle $P_i, P_{i+1}, \dots, P_{i+K-1}$ uniformly at random.</li>
</ul>
<details>
<summary>>>What is the inversion number?<<</summary>
The inversion number of a sequence $(A_1, A_2, \dots, A_N)$ is the number of integer pairs $(i, j)$ satisfying $1 \le i < j \le N$ and $A_i > A_j$.
</details>
<details>
<summary>>>What does "expected value modulo $998244353$" mean?<<</summary>
It can be proved that the sought expected value is always rational. Under the constraints of this problem, when this value is represented as an irreducible fraction $\frac{P}{Q}$, it can also be proved that $Q \not\equiv 0 \pmod{998244353}$. Thus, there is a unique integer $R$ satisfying $R \times Q \equiv P \pmod{998244353}, \ 0 \le R < 998244353$. Report this integer $R$.
</details>
Input
The input is given from Standard Input in the following format:
```
$N$ $K$
$P_1$ $P_2$ $\dots$ $P_N$
```
```
$N$ $K$
$P_1$ $P_2$ $\dots$ $P_N$
```
Output
Print the answer in one line.
Constraints
- All input values are integers.
- $1 \le K \le N \le 2 \times 10^5$
- $P$ is a permutation of $(1,2,\dots,N)$.
Sample 1 Input
4 2
1 4 2 3
Sample 1 Output
166374061
The operation changes the permutation $P$ into the following:
- $(1,4,2,3)$ ... probability $1/2$
- $(4,1,2,3)$ ... probability $1/6$
- $(1,2,4,3)$ ... probability $1/6$
- $(1,4,3,2)$ ... probability $1/6$
The expected value of the inversion number is $\displaystyle 2 \times \frac{1}{2} + 3 \times \frac{1}{6} + 1 \times \frac{1}{6} + 3 \times \frac{1}{6} = \frac{13}{6}$.
$\displaystyle \frac{13}{6}$ modulo $998244353$ is $166374061$, so print this number.
- $(1,4,2,3)$ ... probability $1/2$
- $(4,1,2,3)$ ... probability $1/6$
- $(1,2,4,3)$ ... probability $1/6$
- $(1,4,3,2)$ ... probability $1/6$
The expected value of the inversion number is $\displaystyle 2 \times \frac{1}{2} + 3 \times \frac{1}{6} + 1 \times \frac{1}{6} + 3 \times \frac{1}{6} = \frac{13}{6}$.
$\displaystyle \frac{13}{6}$ modulo $998244353$ is $166374061$, so print this number.
Sample 2 Input
1 1
1
Sample 2 Output
0
Sample 3 Input
10 6
7 4 10 5 6 1 8 2 3 9
Sample 3 Output
499122200