Counting Haybales
Time limit1sMemory limit1024 MB
Given stacks of haybales, a move transfers a top haybale between adjacent stacks whose heights differ by one. Count the reachable configurations modulo 1e9+7.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Sorting, Implementation
- Solved
- No attempts yet
Problem
Bessie the cow is causing trouble in Farmer John's barn again. FJ has () stacks of haybales. For each , the -th stack has () haybales. Bessie does not want any haybales to fall, so the only operation she can perform is the following:
- If two adjacent stacks differ in height by exactly one, she can move the top haybale of the taller stack to the shorter stack.
After performing this operation any finite number of times, how many configurations can be obtained? Print the count modulo . Two configurations are the same if every stack has the same number of haybales in both.
Input
The first line contains (), the number of independent test cases. All of them must be solved to get one input correct.
Each test case consists of , followed by a sequence of heights. The sum of over all test cases does not exceed .
Output
Print lines, one for each test case.
Hint
For the first test case, the four possible configurations are:
For the second test case, the four possible configurations are: