Alice and Bob want to play a game. They have n colored paper strips; the i-th strip is divided into a_i cells numbered from 1 to a_i. Each cell can have one of 3 colors.

In the beginning of the game, Alice and Bob put n chips, the i-th chip is put in the a_i-th cell of the i-th strip. Then they take turns, Alice is first. Each player during their turn has to choose one chip and move it 1, 2 or 3 cells backwards (i. e. if the current cell is x, then the chip can be moved to the cell x - 1, x - 2 or x - 3). There are two restrictions: the chip cannot leave the borders of the strip (for example, if the current cell is 3, then you can't move the chip 3 cells backwards); and some moves may be prohibited because of color of the current cell (a matrix f with size 3 × 3 is given, where f_{i, j} = 1 if it is possible to move the chip j cells backwards from the cell which has color i, or f_{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 998244353.

Input

The first line contains one integer n — the number of paper strips (1 ≤ n ≤ 1000).

The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9), where a_i is the number of cells in the i-th strip.

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

Then m lines follow, each describing an already colored cell. The i-th of these lines contains three integers x_i, y_i and c_i (1 ≤ x_i ≤ n, 1 ≤ y_i ≤ a_{x_i}, 1 ≤ c_i ≤ 3) denoting that the cell y_i in the strip x_i has color c_i. It is guaranteed that if i ≠ j, then either x_i ≠ x_j or y_i ≠ y_j (or both).

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

Output

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

Examples

Input


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


Output


14346


Input


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


Output


1


Input


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


Output


9
#include <bits/stdc++.h>
using namespace std;
const int n = 64;
long long mod = 998244353;
struct Mat {
  long long v[n][n];
  void zero() { fill_n((long long*)v, n * n, 0); }
};
Mat operator*(Mat a, Mat b) {
  Mat r;
  r.zero();
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      for (int k = 0; k < n; k++) (r.v[i][j] += a.v[i][k] * b.v[k][j]) %= mod;
  return r;
}
struct Vec {
  long long v[n];
  void zero() { fill_n(v, n, 0); }
};
Vec operator*(Mat A, Vec b) {
  Vec r;
  r.zero();
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) (r.v[i] += A.v[i][j] * b.v[j]) %= mod;
  return r;
}
int a[1000];
vector<pair<int, int>> colored[1000];
int f[3][3];
Mat powmat[40];
Vec powmul(Vec b, long long p) {
  Vec r = b;
  for (int i = 0; i < 40; i++) {
    if (p >> i & 1) r = powmat[i] * r;
  }
  return r;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int m;
  cin >> m;
  for (int i = 0; i < m; i++) {
    int x, y, c;
    cin >> x >> y >> c;
    x--, y--, c--;
    colored[x].push_back({y, c});
  }
  Mat colmat[3];
  for (int i = 0; i < 3; i++) {
    colmat[i].zero();
    for (int j = 0; j < 3; j++) {
      cin >> f[i][j];
    }
    for (int x = 0; x < 4; x++)
      for (int y = 0; y < 4; y++)
        for (int z = 0; z < 4; z++) {
          set<int> seen;
          if (f[i][0]) seen.insert(z);
          if (f[i][1]) seen.insert(y);
          if (f[i][2]) seen.insert(x);
          int w = 0;
          while (seen.count(w)) w++;
          colmat[i].v[y * 16 + z * 4 + w][x * 16 + y * 4 + z] = 1;
        }
  }
  powmat[0].zero();
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 64; j++)
      for (int k = 0; k < 64; k++)
        (powmat[0].v[j][k] += colmat[i].v[j][k]) %= mod;
  }
  for (int l = 0; l + 1 < 40; l++) {
    powmat[l + 1] = powmat[l] * powmat[l];
  }
  long long ansdp[4] = {1, 0, 0, 0};
  for (int i = 0; i < n; i++) {
    Vec ways;
    ways.zero();
    ways.v[63] = 1;
    sort(colored[i].begin(), colored[i].end());
    int last = 0;
    for (auto p : colored[i]) {
      int x = p.first, c = p.second;
      ways = powmul(ways, x - last);
      ways = colmat[c] * ways;
      last = x + 1;
    }
    ways = powmul(ways, a[i] - last);
    long long answays[4] = {};
    for (int x = 0; x < 4; x++)
      for (int y = 0; y < 4; y++)
        for (int z = 0; z < 4; z++)
          (answays[z] += ways.v[x * 16 + y * 4 + z]) %= mod;
    long long nansdp[4] = {};
    for (int x = 0; x < 4; x++)
      for (int y = 0; y < 4; y++)
        (nansdp[x ^ y] += ansdp[x] * answays[y]) %= mod;
    swap(ansdp, nansdp);
  }
  cout << (ansdp[0] % mod + mod) % mod << endl;
}
