#P6694. 强迫症

    ID: 7372 远端评测题 1000~2000ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>数学2020O2优化生成函数快速数论变换 NTT

强迫症

Background

Little L is a serious OCD patient.

Because of his severe OCD, whenever he draws, he always places the points on a circle.

Problem Description

One day, he asked Little H and Little W a question:

If there are nn distinct points on a circle, labeled from 11 to nn in order, how many ways are there to connect them into a tree?

Little H & Little W: Isn’t this a dumb problem?

Little L: Then what if edges are not allowed to intersect?

Little H & Little W: Isn’t this a dumb problem?

Little L: Then what if we replace “tree” with “graph”?

Little H & Little W: Isn’t this a dumb problem?

Little L: Then what if each point has a weight aia_i, and the weight of an edge (i,j)(i,j) is ai×aja_i\times a_j? Find the expected value of the sum of all edge weights over graphs that satisfy the conditions above.

Little H & Little W: Isn’t this a dumb problem?

After seeing that the problem he had worked on for a long time was instantly solved by a dalao, Little L felt very upset. To comfort him, you need to help solve this problem.

Notes:

  1. Two edges that meet only at an endpoint are not considered intersecting.
  2. A graph with no edges (i.e., only nn points and no edges between them) is also valid.
  3. The points are numbered clockwise from 11 to nn.
  4. The graph cannot contain self-loops or multiple edges.

Input Format

The first line contains a positive integer nn, as described above.

The next line contains nn non-negative integers. The ii-th number is aia_i, the weight of point ii.

Output Format

Output a positive integer representing the answer modulo 998244353998244353.

4
1 1 1 1
665496238
13
1 1 4 5 1 4 1 9 1 9 8 1 0
748867567

Hint

For Sample 1, all 6464 graphs are as follows:

Among them, the 4848 graphs on the left are valid, and the 1616 graphs on the right are invalid. All edge weights are 11.

The expected sum of edge weights is 83\dfrac{8}{3}. Modulo 998244353998244353, the result is 665496238665496238.

Constraints

This problem uses bundled testdata.

  • Subtask 1 ( 10%10\% ): n6n\leq 6.
  • Subtask 2 ( 30%30\% ): n3000n\leq 3000.
  • Subtask 3 ( 60%60\% ): no special restrictions.

For 100%100\% of the data, 2n105,0ai1062\leq n\leq 10^5,0\leq a_i\leq10^6.

The time limit is 1s1s for Subtask 1 and Subtask 2, and 2s2s for Subtask 3.


If you do not know how to take a rational number modulo a prime, please search for “multiplicative inverse”.

Translated by ChatGPT 5