Sum of Circular Sequence Scores
Time limit1sMemory limit1024 MB
Given counts C_a of each value 1 to N in a circular sequence, sum the product of run lengths over all distinct circular arrangements, modulo 1e9+7.
- Level
Hard8 of 10
- Topics
- Combinatorics, Dynamic programming, Math
- Solved
- No attempts yet
Problem
Let be a circular integer sequence of length . Each is adjacent to , and in particular is adjacent to . The score of this circular sequence is the product of the sizes of all contiguous segments of equal numbers. For example, if , the score is .
Write a program that computes the sum of scores over all distinct circular sequences that contain only integers from 1 to , where each integer appears exactly times. Two circular sequences and are different if there exists an with .
Input
The first line contains one integer ().
The second line contains integers (), separated by single spaces.
Output
Print, on the first line, the sum of scores over all distinct circular sequences in which each integer appears exactly times. The sum can be very large, so print it modulo .
Hint
In the first input, the following circular sequences are produced:
- : points
- : point
- : points
- : points
- : point
- : points
The total score is therefore .