#P3290. [SCOI2016] 围棋

[SCOI2016] 围棋

Description

Recently, Google’s Go AI—AlphaGo—defeated the former world champion Lee Sedol by a score of 4:14:1, which is another milestone in the field of artificial intelligence.

Unlike traditional search-based AI, AlphaGo uses the now very popular convolutional neural network model. In a convolutional neural network, every region of a specific size on the board is treated as a window. For example, if the board size is 5×65\times 6 and the window size is 2×42\times 4, then there are 1212 windows on the board. In addition, the model has some predefined templates whose size is the same as the window size.

The following figure shows a 5×65\times 6 board and two 2×42\times 4 templates:

For a template, as long as there exists a window on the board that matches it exactly, we say the template is activated; otherwise, the template is not activated.

For example, in the figure the first template is activated, while the second one is not. The question we study is: for a given template, how many boards can activate it.

To simplify the problem, we disregard all basic rules of Go and consider only an n×mn\times m board, where each position can only be one of three states: black stone, white stone, or empty. In other words, there are 3n×m3^{n\times m} such boards in total. In addition, we will give qq templates of size 2×c2\times c.

We want to know, for each template, how many boards can activate it. Emphasis: templates always have exactly two rows.

Input Format

The first line of input contains four positive integers n,m,cn, m, c and qq, representing the number of rows of the board, the number of columns of the board, the number of columns of the template, and the number of templates, respectively.

Then 2×q2\times q lines follow; every consecutive two lines describe one template. Each line contains cc characters, and each character is one of W, B or X, representing white stone, black stone, or empty, respectively.

Output Format

Output should contain qq lines, each with one integer, representing the number of boards that meet the requirement. Since the answer can be large, you only need to output the result modulo 109+710^9+7.

3 1 1 2
B
W
B
B
6
5

Hint

For all test points: 1n1001\leq n\leq 100, 1m121\leq m\leq 12, 1c61\leq c\leq 6, 1q51\leq q\leq 5.

Test point ID Settings
11 n=3n=3, m=4m=4, c=2c=2
22 n=4n=4, m=4m=4, c=3c=3
33 n=2n=2, m=9m=9, c=6c=6
44 n=2n=2, m=12m=12, c=3c=3
55 n=2n=2, m=12m=12, c=5c=5
66 n=10n=10, m=8m=8, c=3c=3
77 n=10n=10, m=10m=10, c=5c=5
88 n=100n=100, m=10m=10, c=5c=5
99 n=100n=100, m=12m=12, c=5c=5
1010 n=100n=100, m=12m=12, c=6c=6

Translated by ChatGPT 5