Normalizing the Stone Piles
Time limit1sMemory limit1024 MB
Count the permutations of piles for which the greedy rule (the player with fewer stones takes the next pile) leaves both players with equal totals, modulo 1,000,000,007.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Combinatorics, Math
- Solved
- No attempts yet
Problem
Heewon and Chaewan came up with a fun game using stone piles lying on the floor. Pile has stones.
First, they make a permutation of length in which each number from 1 to appears exactly once. Then, over turns in total, they play the game on each turn , from the first turn to the -th turn, as follows.
- If the number of stones Heewon has taken so far is less than Chaewan's, Heewon takes all stones from pile .
- Otherwise, Chaewan takes all stones from pile .
After the game ends, find the number of permutations that make the number of stones taken by the two players equal.
Input
The first line contains the number of stone piles .
The second line contains the numbers of stones in piles through , separated by spaces.
Output
Print the number of permutations that make the two players' totals equal, modulo .