atcoder#ARC126F. [ARC126F] Affine Sort

[ARC126F] Affine Sort

Score : 11001100 points

Problem Statement

Given is a sequence of NN positive integers X=(X1,X2,,XN)X = (X_1, X_2, \ldots, X_N).

For a positive integer KK, let f(K)f(K) be the number of triples of integers (a,b,c)(a,b,c) that satisfy all of the following conditions.

  • 1cK1\leq c \leq K.
  • 0a<c0\leq a < c and 0b<c0\leq b < c.
  • For each ii, let YiY_i be the remainder when aXi+baX_i + b is divided by cc. Then, Y1<Y2<<YNY_1 < Y_2 < \cdots < Y_N holds.

It can be proved that the limit limKf(K)K3\displaystyle \lim_{K\to\infty} \frac{f(K)}{K^3} exists. Find this value modulo 998244353998244353 (see Notes).

Notes

We can prove that the limit in question is always a rational number. Additionally, under the Constraints of this problem, when that number is represented as PQ\frac{P}{Q} using two coprime integers PP and QQ, we can prove that there is a unique integer RR such that R×QP(mod998244353)R\times Q\equiv P\pmod{998244353} and 0R<9982443530\leq R < 998244353. Find this RR.

Constraints

  • 2N1032\leq N\leq 10^3
  • XiX_i are positive integers such that i=1NXi5×105\sum_{i=1}^N X_i \leq 5\times 10^5.
  • XiXjX_i\neq X_j if iji\neq j.

Input

Input is given from Standard Input in the following format:

NN

X1X_1 X2X_2 \ldots XNX_N

Output

Print limKf(K)K3\displaystyle \lim_{K\to\infty} \frac{f(K)}{K^3} modulo 998244353998244353.

3
3 1 2
291154603
  • For example, when (a,b,c)=(3,5,7)(a,b,c) = (3,5,7), we have Y1=0Y_1 = 0, Y2=1Y_2 = 1, Y3=4Y_3 = 4, which satisfy Y1<Y2<Y3Y_1 < Y_2 < Y_3.
  • We have f(1)=0f(1) = 0, f(2)=0f(2) = 0, f(3)=1f(3) = 1, f(4)=2f(4) = 2, f(5)=5f(5) = 5.
  • We have $\displaystyle \lim_{K\to\infty} \frac{f(K)}{K^3} = \frac{1}{24}$.
3
5 9 2
832860616

We have $\displaystyle \lim_{K\to\infty} \frac{f(K)}{K^3} = \frac{55}{1008}$ .

2
2 3
166374059

We have $\displaystyle \lim_{K\to\infty} \frac{f(K)}{K^3} = \frac{1}{6}$.

4
4 5 3 2
0

We have $\displaystyle \lim_{K\to\infty} \frac{f(K)}{K^3} = 0$.