#P16535. [THUPC 2026 决赛] 供电网络

[THUPC 2026 决赛] 供电网络

Background

From the final of the 2026 Tsinghua University Student Programming Contest and Intercollegiate Invitational (THUPC2026).

Resources such as the editorial can be found at https://github.com/dapingguo8/THUPC2026-final.

After finishing the yearbook, the venue was preparing to start the nighttime lighting effects. However, since the backstage power supply network had not been configured yet, the long-awaited holographic projector still could not be turned on.

The entire power grid consists of many relay nodes, and these nodes must be connected to two parallel main power modules. According to the electrical safety protocol, adjacent nodes within the same module are very likely to cause phase resonance. Therefore, the system has a strict parity restriction on the number of neighbors that each node has within its own module.

Facing the complicated transmission lines, Xiao S pulled out a stack of old test records. Xiao T pointed out that, since the tests back then were carried out level by level along the hierarchy of the grid, the node sets involved in the records have a regular nested structure: for any two records, the node sets they involve are either completely disjoint, or one strictly contains the other.

Blind trial and error is not only time-consuming but also highly risky. In order to configure the power supply network as soon as possible, Xiao T and Xiao S need to restore in advance, for each test, the number of ways to connect all nodes to the main modules.

Problem Description

The power supply network contains nn nodes. Nodes are connected by several bidirectional transmission lines, forming an undirected graph.

When configuring the network, all nodes will be assigned to two independent main power modules. For node i (1≤i≤n)i \ (1 \le i \le n), define its same-module neighbor count did_i as: within the power module that node ii is connected to, the number of nodes that have a direct line connected to node ii.

Xiao S found records of qq tests. Each test record is represented by a string ss of length nn. For node i (1≤i≤n)i \ (1 \le i \le n):

  • If si=‘0’s_i = \text{`0'}, then in this configuration, node ii must have an even same-module neighbor count did_i;
  • If si=‘1’s_i = \text{`1'}, then in this configuration, node ii must have an odd same-module neighbor count did_i;
  • If si=‘?’s_i = \text{`?'}, then node ii is not involved in the record, i.e. there is no requirement on the parity of did_i.

Xiao T pointed out that the node sets involved in the records have a regular nested structure. Specifically, let the node set involved in the ii-th test (1≤i≤q)(1 \le i \le q) be SiS_i (i.e. the set of positions in the string that are not ?). Then for any two different test records i,j (1≤i<j≤q)i, j \ (1 \le i < j \le q), SiS_i and SjS_j must satisfy exactly one of the following three relations: Si⊆SjS_i \subseteq S_j, Sj⊆SiS_j \subseteq S_i, or Si∩Sj=∅S_i \cap S_j = \varnothing.

To configure the power supply network as soon as possible, you need to help Xiao T and Xiao S compute, for each test, the number of essentially different ways to connect all nodes to the two main modules. Two assignments are considered different if and only if there exists at least one node that is connected to different main modules in the two assignments. Since the answer may be large, you only need to output it modulo 109+710 ^ 9 + 7.

Input Format

The first line contains two positive integers n,q (1≤n,q≤3×103)n, q \ (1 \le n, q \le 3 \times 10 ^ 3).

The next nn lines each contain a 0101 string of length nn. The j (1≤j≤n)j \ (1 \le j \le n)-th character of the i (1≤i≤n)i \ (1 \le i \le n)-th line indicates whether there is a transmission line between node ii and node jj: 1 if there is, otherwise 0.

The next qq lines each contain a string ss of length nn, representing one test record.

Output Format

Output qq lines. Each line contains a non-negative integer, representing the number of essentially different ways to connect all nodes to the two main modules in that test, modulo 109+710 ^ 9 + 7.

3 2
010
100
000
1?0
010
4
0
6 5
000010
000001
000000
000001
100000
010100
?11?0?
??????
?10?1?
??0?0?
?01?01
0
64
16
32
0

Hint

For the first test in Sample 1, there are four possible connection assignments:

  1. Connect all nodes to the first main module.
  2. Connect all nodes to the second main module.
  3. Connect nodes 1,21, 2 to the first main module, and connect node 33 to the second main module.
  4. Connect nodes 1,21, 2 to the second main module, and connect node 33 to the first main module.

Translated by ChatGPT 5