Let Me Tell You a Story (Large)
Time limit30sMemory limit512 MB
Count the orders in which ministers can be fired while a salary complaint remains possible until the survivors are non-increasing, modulo 10007.
- Level
Hard8 of 10
- Topics
- Combinatorics, Dynamic programming
- Solved
- No attempts yet
Problem
Let me tell you a story about King Tyrone the Fair.
King Tyrone the Fair had ministers, ranked from the first minister down to the -th minister. The -th minister was paid gold pieces per week.
One day the salary list became public. From then on, whenever a minister was paid less than some minister ranked below him, that minister complained, and the king fired exactly one of the ministers still in office. The king never changed a salary and never changed a rank. The minister he fired did not have to be the one who complained or the one who caused the complaint. It could be any minister still in office, even one with nothing to do with the problem. The ministers who stay keep their relative ranks. Firings went on as long as a complaint was possible, and stopped the moment the salaries of the remaining ministers were non-increasing from the highest rank to the lowest.
One story for the salaries goes like this. The second minister complained, so the king fired the first minister and remained. The second minister complained again, so the king fired him and remained. Nobody complains about , so the firings stopped.
I remember the salaries, but not the order in which the ministers were fired. Count how many stories I could tell. Two stories are different if the sequences of fired ministers are different. Ministers are told apart by their original rank, even when their salaries are equal. If nobody can complain at the start, no minister is ever fired and the only story is the empty sequence.
Print the count modulo .
Input
The first line contains the number of test cases . Each test case takes two lines. The first line contains , and the second line contains space separated integers , the salaries from the first minister to the -th minister.
Constraints
Output
For each test case, print one line of the form Case #x: y, where is the test case number starting from 1 and is the number of stories modulo .