#P6151. [集训队作业2019] 青春猪头少年不会梦到兔女郎学姐
[集训队作业2019] 青春猪头少年不会梦到兔女郎学姐
Description
Several positive integers are arranged into a sequence. The number of occurrences of integer is . For each such sequence, define its weight as follows:
Connect the head and tail of the sequence to form a circle. Divide these numbers into several adjacent segments such that each segment consists of consecutive numbers on the circle, any two segments have no elements in common, all numbers within each segment are the same, and the numbers in adjacent segments are different. Then the weight of this sequence is defined as the product of the lengths of all segments.
Compute the sum of the weights of all sequences, modulo .
Note: Although the weight is computed on a circular arrangement, sequences that are cyclic shifts of each other are still considered different. For example, and are considered different sequences.
Input Format
Multiple lines. The first line contains a positive integer , the number of distinct values.
The second line contains positive integers , where is the number of occurrences of the -th value.
Output Format
One line, the sum of the weights of all sequences whose occurrence counts satisfy the conditions, modulo .
2
2 2
18
6
7 8 9 10 11 12
515320459
Hint
Sample Explanation #1:
The valid sequences are $(1,1,2,2),(1,2,1,2),(1,2,2,1),(2,1,1,2),(2,1,2,1),(2,2,1,1)$. Their weights are , and the sum is .
Constraints:
Translated by ChatGPT 5
京公网安备 11011102002149号