#P7869. Permutation Counting

Permutation Counting

Permutation Counting

Problem Description

For a given permutation a1,a2,,ana_1, a_2, \cdots, a_n of length nn, we defined the neighbor sequence bb of aa, the length of which is n1n-1, as following:

$$b_i= \begin{cases}0~~~a_i\text{ < } a_{i+1} \\ 1~~~a_i>a_{i+1}\end{cases} $$

For example, the neighbor sequence of permutation 1,2,3,6,4,51,2,3,6,4,5 is 0,0,0,1,00,0,0,1,0. Now we give you an integer nn and a sequence b1,b2,,bn1b_1, b_2, \cdots, b_{n-1} of length n1n-1, you should calculate the number of permutations of length nn whose neighbor sequence equals to bb. To avoid calculation of big number, you should output the answer module 109+710^9+7.

Input

The first line contains one positive integer TT (1T501\le T \le 50), denoting the number of test cases. For each test case: The first line of the input contains one integer n,(2n5000)n, (2 \le n \le 5000). The second line of the input contains n1n - 1 integer: b1,b2,,bn1b_1, b_2, \cdots, b_{n - 1} There are no more than 2020 cases with n>300n > 300.

Output

For each test case: Output one integer indicating the answer module 109+710^9 + 7.

Sample Input

2
3
1 0
5
1 0 0 1

Sample Output

2
11

Source

2020 Multi-University Training Contest 10