Page not loading? Try clicking here.
Placeholder

#10406

Hidden Pancakes 30s 1024MB

Problems

We are cooking \mathbf{N} pancakes in total. We cook one pancake with a 1 centimeter (cm) radius, one with a 2 cm radius, one with a 3 cm radius, ..., and one with an \mathbf{N} cm radius, not necessarily in that order. After we cook the first pancake, we just lay it on a plate. After we cook each subsequent pancake, we lay it on top of the previously made pancake, with their centers coinciding. In this way, a pancake is visible from the top of the stack when we first add it. A pancake only becomes hidden if we later cook another pancake with a larger radius.

For example, say we cook 4 pancakes. We first cook the pancake with radius 3 cm, and it is visible. Then, we cook the pancake with radius 1 cm, lay it on top of the first one and both are visible. Third, we cook the pancake with radius 2 cm, and now that covers the previous pancake, but not the first one, so 2 pancakes remain visible in total. Finally, we cook the pancake with radius 4 cm which covers the other pancakes leaving only 1 visible pancake. The picture below illustrates the state of the stack after each pancake is cooked. Within each stack, the fully colored pancakes are visible and the semi-transparent pancakes are not visible.

Four stacks with pancakes of radii 3, 3 and 1, 3, 1 and 2, and 3, 1, 2 and 4.

Let \mathbf{V_i} be the number of visible pancakes when the stack contains exactly i pancakes. In the example above, \mathbf{V_1} = 1, \mathbf{V_2} = 2, \mathbf{V_3} = 2, and \mathbf{V_4} = 1.

Given the list \mathbf{V_1}, \mathbf{V_2}, \dots, \mathbf{V_N}, how many of the \mathbf{N}! possible cooking orders yield those values? Since the output can be a really big number, we only ask you to output the remainder of dividing the result by the prime 10^9+7 (1000000007).


Input

The first line of the input gives the number of test cases, \mathbf{T}. \mathbf{T} test cases follow, each described with two lines. The first line of a test case contains a single integer \mathbf{N}, the number of pancakes we cook. The second line of a test case contains \mathbf{N} integers \mathbf{V_1}, \mathbf{V_2}, ..., \mathbf{V_N}, representing the number of visible pancakes after we cook 1, 2, ..., \mathbf{N} pancakes, respectively.


Output

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the number of cooking orders of \mathbf{N} pancakes that yield the given numbers of visible pancakes after each step, modulo the prime 10^9+7 (1000000007).


Example #1

3
4
1 2 2 1
3
1 1 2
3
1 1 3
Case #1: 1
Case #2: 2
Case #3: 0
In the Sample Case for Test Set 2, there are 316234143225 cooking orders that yield the given \mathbf{V_i}s. Modulo 10^9+7, this value is 234141013.

Example #2

1
24
1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2
Case #1: 234141013

Source

GCJ 2021r2 C

You must sign in to write code.