8510: link with Bracket Sequence II

内存限制:128 MB 时间限制:1 S
题面:传统 评测方式:文本比较 上传者:
提交:1 通过:1

题目描述

link has a bracket sequence of length n. Today, link finds out that some brackets of the sequence were lost.

Of course, he wants you to calculate how many ways are there to fill the sequence so that it will be a valid bracket sequence.

Note that there are m types of brackets in link's world.

Here's the definition of a valid bracket sequence:

· A sequence of length 0 is a valid bracket sequence. · If A is a valid bracket sequence, x is some type of left bracket, y is the same type of right bracket, xAy is a valid bracket sequence. · If A,B are both valid bracket sequences, then AB is also a valid bracket sequence.

输入格式

Each test contains multiple test cases. The first line contains the number of test casesT(1T20). Description of the test cases follows.

The first line contains two integers n(1n500),m(1m<109+7), which is the length of link's bracket sequence and the types of brackets.

The second line contains n integers a1,a2,,an(aim). The i-th integer ai describes the i-th character in the sequence:

· If ai=0, it means the bracket in this position is lost. · ai>0, it means the i-th character in the sequence is the ai-th type of left bracket. ·ai<0, it means the ii-th character in the sequence is the -aith type of right bracket.

It is guaranteed that there are at most 15 test cases with n>100.

输出格式

For each test case, output one integer in a line, which is the number of ways to fill the bracket sequence so that it is a valid bracket sequence. Since the answer can be huge, just print it modulo 109+7.

For some reason, there could be no way to make it a valid bracket sequence.

输入样例 复制

3
4 2
1 0 0 -1
4 2
0 0 0 -1
6 3
0 0 0 0 0 0

输出样例 复制

3
4
135