#abc326e. E - Revenge of "The Salary of AtCoder Inc."

E - Revenge of "The Salary of AtCoder Inc."

Score : 450450 points

问题描述

Aoki 是 AtCoder Inc. 的一名员工,本月的工资由一个整数 NN 和长度为 NN 的序列 AA 如下确定:

首先,他获得了一个 NN 面骰子(投掷一次可显示从 11NN 的整数,每个数字出现的概率相等),并初始化变量 x=0x=0

然后,重复执行以下步骤直至结束:

  • 投掷骰子一次,得到的结果记为 yy
    • x<yx<y,则支付给他 AyA_y 日元,并令 x=yx=y
    • 否则,终止该过程。

Aoki 本月的工资即通过此过程累计支付的总额。
求 Aoki 本月工资的期望值,结果对 998244353998244353 取模。

如何求取模 998244353998244353 下的期望值:
可以证明本问题中所求期望值始终是一个有理数。同时,本题的约束条件保证了若将所求期望值表示为简化分数 yx\frac yx,则 xx 不会被 998244353998244353 整除。此时存在唯一的一个满足 0z<9982443530\leq z<998244353 的整数 zz,使得 yxz(mod998244353)y\equiv xz\pmod{998244353}。请输出这个 zz

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

Aoki, an employee at AtCoder Inc., has his salary for this month determined by an integer NN and a sequence AA of length NN as follows.
First, he is given an NN-sided die (dice) that shows the integers from 11 to NN with equal probability, and a variable x=0x=0.

Then, the following steps are repeated until terminated.

  • Roll the die once and let yy be the result.
    • If x<yx<y, pay him AyA_y yen and let x=yx=y.
    • Otherwise, terminate the process.

Aoki's salary for this month is the total amount paid through this process.
Find the expected value of Aoki's salary this month, modulo 998244353998244353.

How to find an expected value modulo 998244353998244353 It can be proved that the sought expected value in this problem is always a rational number. Also, the constraints of this problem guarantee that if the sought expected value is expressed as a reduced fraction yx\frac yx, then xx is not divisible by 998244353998244353. Here, there is exactly one 0z<9982443530\leq z\lt998244353 such that yxz(mod998244353)y\equiv xz\pmod{998244353}. Print this zz.

Constraints

  • All inputs are integers.
  • 1N3×1051 \le N \le 3 \times 10^5
  • 0Ai<9982443530 \le A_i < 998244353

Input

The input is given from Standard Input in the following format:

NN

A1A_1 A2A_2 \dots ANA_N

Output

Print the answer.

Sample Input 1

3
3 2 6

Sample Output 1

776412280

Here is an example of how the process goes.

  • Initially, x=0x=0.
  • Roll the die once, and it shows 11. Since 0<10<1, pay him A1=3A_1 = 3 yen and let x=1x=1.
  • Roll the die once, and it shows 33. Since 1<31<3, pay him A3=6A_3 = 6 yen and let x=3x=3.
  • Roll the die once, and it shows 11. Since 313 \ge 1, terminate the process.

In this case, his salary for this month is 99 yen.

It can be calculated that the expected value of his salary this month is 499\frac{49}{9} yen, whose representation modulo 998244353998244353 is 776412280776412280.

Sample Input 2

1
998244352

Sample Output 2

998244352

Sample Input 3

9
3 14 159 2653 58979 323846 2643383 27950288 419716939

Sample Output 3

545252774

update @ 2024/3/10 01:49:54