Permutation Counting

Permutation Counting

Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)

Problem Description

For a given permutation a 1 , a 2 , ⋯ , a n a_1,a_2,⋯,a_n a1,a2,,an of length n n n, we defined the neighbor sequence b b b of a a a, the length of which is n − 1 n−1 n1, as following:

b i = { 0 a i < a i + 1 1 a i > a i + 1 b_i= \begin{cases} 0 &a_i<a_{i+1}\\ 1 & a_i>a_{i+1} \end{cases} bi={01ai<ai+1ai>ai+1

For example, the neighbor sequence of permutation 1 , 2 , 3 , 6 , 4 , 5 1,2,3,6,4,5 1,2,3,6,4,5 is 0 , 0 , 0 , 1 , 0 0,0,0,1,0 0,0,0,1,0.

Now we give you an integer n n n and a a a sequence b 1 , b 2 , ⋯ , b n − 1 b_1,b_2,⋯,b_{n−1} b1,b2,,bn1 of length n − 1 n−1 n1, you should calculate the number of permutations of length n n n whose neighbor sequence equals to b b b.

To avoid calculation of big number, you should output the answer module 1 0 9 + 7 10^9+7 109+7.

Input

The first line contains one positive integer T ( 1 ≤ T ≤ 50 ) T (1≤T≤50) T(1T50), denoting the number of test cases. For each test case:

The first line of the input contains one integer n , ( 2 ≤ n ≤ 5000 ) n,(2≤n≤5000) n,(2n5000).

The second line of the input contains n − 1 n−1 n1 integer: b 1 , b 2 , ⋯ , b n − 1 b_1,b_2,⋯,b_{n−1} b1,b2,,bn1

There are no more than 20 20 20 cases with n > 300 n>300 n>300.

Output

For each test case:

Output one integer indicating the answer module 1 0 9 + 7 10^9+7 109+7.

Sample Input

2
3
1 0
5
1 0 0 1

Sample Output

2
11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值