Quark the corgi has a bin full of balls, each of which has an integer written on it. The integers form a multiset $$$S=\{b_1,b_2,\dots,b_n\}$$$, and he wants to use it to generate a random sequence $$$A=a_1,a_2,\dots,a_n$$$, that he will later use to decide in what order to fetch the balls.
Initially, the sequence $$$A$$$ is empty. Quark performs the following operation exactly $$$n$$$ rounds:
For the $$$i$$$-th round, let $$$S_i$$$ be the multiset of the remaining numbers after previous rounds ($$$S_i=S$$$ if $$$i=1$$$). Let $$$sum_i$$$ be the sum of all elements in $$$S_i$$$. Quark will randomly select an element from $$$S_i$$$ with probability proportional to its value. Specifically, the probability of selecting $$$b_j\in S_i$$$ is $$$\frac{b_j}{sum_i}$$$. Then Quark will append the selected number to the end of the sequence $$$A$$$ and remove it from $$$S_i$$$.
After all $$$n$$$ rounds, Quark obtains a random sequence $$$A$$$. He is interested in the expected number of inversions in $$$A$$$, i.e., the expected number of index pairs $$$(i, j)$$$ such that $$$i \lt j$$$ and $$$a_i \gt a_j$$$.
Since corgis are known to dislike fractions, you only need to output the answer modulo $$$998244353$$$.
The first line contains a single integer $$$n\ (2\le n\le 10^6)$$$, representing the size of the multiset $$$S$$$.
The second line contains $$$n$$$ integers $$$b_1,b_2,\cdots,b_n\ (1\le b_i\le 10^5)$$$, representing the elements in $$$S$$$.
A single integer, representing the expected number of inversions in the random sequence $$$A$$$, modulo $$$998244353$$$.
31 2 3
715408455
32 3 3
598946613
For the first example, the table below shows the number of inversions and the corresponding probability to generate it for each possible sequence $$$A$$$.
| $$$A$$$ | Number of inversions | Probability |
| $$$1,2,3$$$ | $$$0$$$ | $$$\frac{1}{15}$$$ |
| $$$1,3,2$$$ | $$$1$$$ | $$$\frac{1}{10}$$$ |
| $$$2,1,3$$$ | $$$1$$$ | $$$\frac{1}{12}$$$ |
| $$$2,3,1$$$ | $$$2$$$ | $$$\frac{1}{4}$$$ |
| $$$3,1,2$$$ | $$$2$$$ | $$$\frac{1}{6}$$$ |
| $$$3,2,1$$$ | $$$3$$$ | $$$\frac{1}{3}$$$ |
Thus, the expected number of inversions equals $$$\frac{1}{10} + \frac{1}{12} + 2 \times \frac{1}{4} + 2\times \frac{1}{6} + 3\times \frac{1}{3} = \frac{121}{60}$$$, which equals $$$715408455$$$ modulo $$$998244353$$$.
| Название |
|---|


