Contest Strategy
Time limit2sMemory limit512 MB
Sum the contest penalty over all n! read orders, where after reading k problems you always solve the read-but-unsolved problem with the smallest solving time.
- Level
Hard8 of 10
- Topics
- Combinatorics, Greedy, Sorting, Math
- Solved
- No attempts yet
Problem
You are taking part in the ACM International Collegiate Programming Contest (ICPC), where you have to solve all problems. Reading a problem takes no time, and once you have read it you know exactly how long solving it takes. Solving problem takes .
Your strategy during the contest is this.
- Read random problems.
- Among the problems you have read but not solved, choose one whose solving time is smallest. If several of them tie, choose any one.
- Solve it, then read one random unread problem if any is left.
- If unsolved problems remain, go back to step 2.
The submission time of a problem is the time that has passed when you finish solving it, that is, the total solving time of every problem you have solved up to and including that one. The penalty of the contest is the sum of the submission times of all problems.
One read order is one permutation of the problems. You read the first problems of the permutation at the start, and after solving a problem you read the next problem of the permutation. The penalty depends on the read order, and there are read orders. Add up the penalty over all of them and print the sum modulo .
When several read problems tie for the smallest solving time in step 2, the penalty is the same whichever one you choose, so the answer is uniquely determined.
Input
The first line contains two integers and separated by a space ().
The -th of the next lines contains one integer ().
Output
Print on one line the sum of the penalties over all read orders, modulo .