#P6694. 强迫症
强迫症
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 distinct points on a circle, labeled from to 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 , and the weight of an edge is ? 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:
- Two edges that meet only at an endpoint are not considered intersecting.
- A graph with no edges (i.e., only points and no edges between them) is also valid.
- The points are numbered clockwise from to .
- The graph cannot contain self-loops or multiple edges.
Input Format
The first line contains a positive integer , as described above.
The next line contains non-negative integers. The -th number is , the weight of point .
Output Format
Output a positive integer representing the answer modulo .
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 graphs are as follows:

Among them, the graphs on the left are valid, and the graphs on the right are invalid. All edge weights are .
The expected sum of edge weights is . Modulo , the result is .
Constraints
This problem uses bundled testdata.
- Subtask 1 ( ): .
- Subtask 2 ( ): .
- Subtask 3 ( ): no special restrictions.
For of the data, .
The time limit is for Subtask 1 and Subtask 2, and 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