#P17421. [ICPC 2018 Xuzhou R] Rikka with Subsequences

    ID: 19923 远端评测题 6000ms 512MiB 尝试: 0 已通过: 0 显示难度提高+/省选− 上传者: 标签>动态规划 DP2018动态规划优化ICPC

[ICPC 2018 Xuzhou R] Rikka with Subsequences

Problem Description

For a known sequence, counting subsequences with a certain remarkable property can depict the sequence itself to a certain extent.

Now, Rikka has a sequence AA of length nn whose elements, denoted by a1,a2,⋯ ,ana_1, a_2, \cdots, a_n, are positive integers in [1,n][1, n]. A bridging relation matrix (BRM) is a n×nn \times n logical matrix with elements from {0,1}\{0, 1\}. Here Rikka defines a Yuta subsequence based on a given BRM, M=(Mi,j)1≤i,j≤nM = (M_{i, j})_{1 \le i, j \le n}.

Rikka calls a subsequence of AA, denoted by ap1,ap2,⋯ ,apma_{p_1}, a_{p_2}, \cdots, a_{p_m} with m≥1m\ge 1 and 1≤p1<p2<⋯<pm≤n1 \le p_1 < p_2 < \cdots < p_m \le n, a Yuta subsequence if and only if Mapi,api+1=1M_{a_{p_i}, a_{p_{i + 1}}} = 1 for i=1,2,⋯ ,m−1i = 1, 2, \cdots, m - 1. Counting the number of different Yuta subsequences has a profound value in data analysis and data recovery.

Rikka thinks this task is too simple and she wants to make it look harder and more heuristic. Rikka knows that a Yuta subsequence may appear in the sequence AA several times and top programmers may use something like map<vector<int>, bigInt> cnt in C++ or Map<ArrayList<Integer>, BigInteger> cnt in Java to store all Yuta subsequences and count the numbers.

She calls the sum of the cubes of the numbers of occurrences for all Yuta subsequences, which is equal to the sum of cubes of all the second elements in cnt, the third coefficient of AA over MM.

Now, after showing you the sequence and the BRM, she wants you to calculate the third coefficient of the sequence over the given BRM in modulo (109+7)(10^9 + 7).

Input Format

The input contains several test cases, and the first line contains a single integer TT (1≤T≤201 \le T \le 20), the number of test cases.

For each test case, the first line contains a single integer nn (1≤n≤2001 \le n \le 200), the length of the sequence AA.

The second line contains nn integers a1,a2,⋯ ,ana_1, a_2, \cdots, a_n (1≤ai≤n1 \le a_i \le n).

The following nn lines describe the given BRM, where each line of them has nn characters, and the jj-th character in the ii-th line of them is either 00 or 11, representing the element Mi,jM_{i, j}.

Output Format

For each test case, output a single line with a single integer, the third coefficient of the given sequence over the given BRM in modulo (109+7)(10^9 + 7).

1
4
1 2 1 2
1111
1111
1111
1111
51