Under construction

Still being built. Things may change

Inversion Number Squared

Time limit2sMemory limit1024 MB

Summary
Given a_i copies of each value i from 1 to n, find the expected square of the inversion count of a uniformly random arrangement, modulo 998244353.
Level

Hard8 of 10

Topics
Probability, Combinatorics, Math
Solved
No attempts yet

Problem

Consider sequences made of a1a_1 copies of 11, a2a_2 copies of 22, …\dots, and ana_n copies of nn. Pick one of these sequences uniformly at random. Find the expected value of the square of its inversion count, modulo 998 244 353998\,244\,353.

The inversion count of a sequence p1,p2,…,pnp_1, p_2, \dots, p_n is the number of pairs (i,j)(i,j) with i<ji<j and pi>pjp_i>p_j.

Input

The first line contains nn, the maximum value in the sequence. (2≤n≤200 0002 \leq n \leq 200\,000)

The second line contains aia_i, the number of copies of each value ii, separated by spaces. (1≤ai1 \leq a_i; n≤∑i=1nai≤998 244 352n \leq \sum_{i=1}^{n} a_i \leq 998\,244\,352)

Output

Print the answer modulo 998 244 353998\,244\,353.

Hint

The expected value of the square of the inversion count is a rational number. Write it as pq\frac{p}{q} with pp and qq coprime. You can prove that qq is not a multiple of 998 244 353998\,244\,353. The answer is the smallest non-negative integer aa that satisfies aq≡p(mod998 244 353)aq \equiv p \pmod{998\,244\,353}.

There are two sequences made of one 11 and one 22: 1212 and 2121. Their inversion counts are 00 and 11, so the expected square is 12\frac{1}{2}. Since 499 122 177×2≡1(mod998 244 353)499\,122\,177 \times 2 \equiv 1 \pmod{998\,244\,353}, the answer is 499 122 177499\,122\,177.

Examples2

  1. Example 1

    Input
    2
    1 1
    
    Expected output
    499122177
    
  2. Example 2

    Input
    5
    3 1 4 1 1
    
    Expected output
    166374411