#P8354. [SDOI/SXOI2022] 多边形
[SDOI/SXOI2022] 多边形
Problem Description
Given a regular -gon, in addition to its vertices, on the -th edge in clockwise order there are also extra vertices that divide this segment into equal parts. That is, the -th edge is divided by vertices into segments of equal length.
You may connect some line segments between vertices. After adding these segments, any two newly added segments may intersect only at endpoints. Also, no new segment should overlap with any edge of the polygon.
We call the resulting graph after adding some segments a triangulation if and only if every face inside the polygon is a triangle. Note that the sides of a triangle may contain vertices that lie on the original edges of the convex polygon.
Given such a convex polygon, how many triangulations satisfy the conditions above? You only need to output the number of valid triangulations modulo .
Input Format
The first line contains a positive integer , denoting the number of sides of the convex polygon.
The second line contains positive integers. The -th integer is , with the meaning described above.
Output Format
Output one integer in one line, the number of valid triangulations modulo .
3
2 2 1
5
5
3 1 4 2 5
359895
8
4 2 1 8 3 7 3 1
577596154
Hint
[Sample 1 Explanation]
There are valid solutions, as shown in the figure.

[Constraints]
For of the testdata, .
For of the testdata, .
For of the testdata, , , .
Note: There is no large sample download for this problem.
Translated by ChatGPT 5