#agc023e. [agc023_e]Inversions
[agc023_e]Inversions
Problem Statement
Snuke has an integer sequence whose length is . He likes permutations of , , that satisfy the following condition:
- for all ( ).
Snuke is interested in the inversion numbers of such permutations. Find the sum of the inversion numbers over all permutations that satisfy the condition. Since this can be extremely large, compute the sum modulo .
Notes
The inversion number of a sequence whose length is the number of pairs of integers and ( ) such that .
Constraints
- ( )
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the sum of the inversion numbers over all permutations that satisfy the condition.
Sample Input 1
3
2 3 3
Sample Output 1
4
There are four permutations that satisfy the condition: , , and . The inversion numbers of these permutations are , , and , respectively, for a total of .
Sample Input 2
6
4 2 5 1 6 3
Sample Output 2
7
Only one permutation satisfies the condition. The inversion number of this permutation is , so the answer is .
Sample Input 3
5
4 4 4 4 4
Sample Output 3
0
No permutation satisfies the condition.
Sample Input 4
30
22 30 15 20 10 29 11 29 28 11 26 10 18 28 22 5 29 16 24 24 27 10 21 30 29 19 28 27 18 23
Sample Output 4
848414012