#P1197F. Coloring Game

Coloring Game

Description

Alice and Bob want to play a game. They have nn colored paper strips; the ii-th strip is divided into aia_i cells numbered from 11 to aia_i. Each cell can have one of 33 colors.

In the beginning of the game, Alice and Bob put nn chips, the ii-th chip is put in the aia_i-th cell of the ii-th strip. Then they take turns, Alice is first. Each player during their turn has to choose one chip and move it 11, 22 or 33 cells backwards (i. e. if the current cell is xx, then the chip can be moved to the cell x1x - 1, x2x - 2 or x3x - 3). There are two restrictions: the chip cannot leave the borders of the strip (for example, if the current cell is 33, then you can't move the chip 33 cells backwards); and some moves may be prohibited because of color of the current cell (a matrix ff with size 3×33 \times 3 is given, where fi,j=1f_{i, j} = 1 if it is possible to move the chip jj cells backwards from the cell which has color ii, or fi,j=0f_{i, j} = 0 if such move is prohibited). The player who cannot make a move loses the game.

Initially some cells may be uncolored. Bob can color all uncolored cells as he wants (but he cannot leave any cell uncolored). Let's call a coloring good if Bob can win the game no matter how Alice acts, if the cells are colored according to this coloring. Two colorings are different if at least one cell is colored in different colors in these two colorings.

Bob wants you to calculate the number of good colorings. Can you do it for him?

Since the answer can be really large, you have to print it modulo 998244353998244353.

The first line contains one integer nn — the number of paper strips (1n10001 \le n \le 1000).

The second line contains nn integers a1a_1, a2a_2, ..., ana_n (1ai1091 \le a_i \le 10^9), where aia_i is the number of cells in the ii-th strip.

The third line contains one integer mm (1m10001 \le m \le 1000) — the number of cells that are already colored.

Then mm lines follow, each describing an already colored cell. The ii-th of these lines contains three integers xix_i, yiy_i and cic_i (1xin1 \le x_i \le n, 1yiaxi1 \le y_i \le a_{x_i}, 1ci31 \le c_i \le 3) denoting that the cell yiy_i in the strip xix_i has color cic_i. It is guaranteed that if iji \ne j, then either xixjx_i \ne x_j or yiyjy_i \ne y_j (or both).

Then 33 lines follow, ii-th line containing 33 numbers fi,1f_{i, 1}, fi,2f_{i, 2}, fi,3f_{i, 3} (0fi,j10 \le f_{i, j} \le 1). If fi,j=1f_{i, j} = 1, then it is possible to move the chip jj cells backwards from the cell having color ii; if fi,j=0f_{i, j} = 0, then such move is impossible.

Print one integer: the number of good colorings, taken modulo 998244353998244353.

Input

The first line contains one integer nn — the number of paper strips (1n10001 \le n \le 1000).

The second line contains nn integers a1a_1, a2a_2, ..., ana_n (1ai1091 \le a_i \le 10^9), where aia_i is the number of cells in the ii-th strip.

The third line contains one integer mm (1m10001 \le m \le 1000) — the number of cells that are already colored.

Then mm lines follow, each describing an already colored cell. The ii-th of these lines contains three integers xix_i, yiy_i and cic_i (1xin1 \le x_i \le n, 1yiaxi1 \le y_i \le a_{x_i}, 1ci31 \le c_i \le 3) denoting that the cell yiy_i in the strip xix_i has color cic_i. It is guaranteed that if iji \ne j, then either xixjx_i \ne x_j or yiyjy_i \ne y_j (or both).

Then 33 lines follow, ii-th line containing 33 numbers fi,1f_{i, 1}, fi,2f_{i, 2}, fi,3f_{i, 3} (0fi,j10 \le f_{i, j} \le 1). If fi,j=1f_{i, j} = 1, then it is possible to move the chip jj cells backwards from the cell having color ii; if fi,j=0f_{i, j} = 0, then such move is impossible.

Output

Print one integer: the number of good colorings, taken modulo 998244353998244353.

Sample Input 1

3
3 4 5
2
1 1 1
2 2 2
1 1 1
1 0 0
0 1 1

Sample Output 1

14346

Sample Input 2

1
1
1
1 1 1
1 1 1
1 1 1
1 1 1

Sample Output 2

1

Sample Input 3

3
1 1 1
1
1 1 1
1 1 1
1 1 1
1 1 1

Sample Output 3

9