#include <bits/stdc++.h>
using namespace std;
const int Mod = 998244353;
struct Matrix {
  int a[64][64];
  Matrix() { memset(a, 0, sizeof a); }
  Matrix operator*(Matrix b) {
    Matrix c;
    for (int i = 0; i <= 63; i++)
      for (int j = 0; j <= 63; j++)
        for (int k = 0; k <= 63; k++)
          c.a[i][j] = (c.a[i][j] + 1ll * a[i][k] * b.a[k][j]) % Mod;
    return c;
  }
} mov, pw[30];
struct Vector {
  int a[64];
  Vector() { memset(a, 0, sizeof a); }
  Vector operator*(Matrix b) {
    Vector c;
    for (int i = 0; i <= 63; i++)
      for (int j = 0; j <= 63; j++)
        c.a[i] = (c.a[i] + 1ll * a[j] * b.a[j][i]) % Mod;
    return c;
  }
} res[1005];
Vector cal(Vector v, int p) {
  for (int i = 0; i <= 29; i++)
    if (p & 1 << i) v = v * pw[i];
  return v;
}
int m, n, a[1005], f[4][4], cnt[1005][4];
struct rec {
  int x, y, c;
} inp[1005];
bool cmp(rec a, rec b) {
  if (a.x != b.x) return a.x < b.x;
  return a.y < b.y;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  cin >> m;
  for (int i = 1; i <= m; i++) cin >> inp[i].x >> inp[i].y >> inp[i].c;
  sort(inp + 1, inp + 1 + m, cmp);
  for (int i = 1; i <= 3; i++)
    for (int j = 1; j <= 3; j++) cin >> f[i][j];
  for (int i = 0; i <= 3; i++)
    for (int j = 0; j <= 3; j++)
      for (int k = 0; k <= 3; k++)
        for (int w = 1; w <= 3; w++) {
          bool vis[4] = {0};
          if (f[w][1]) vis[k] = true;
          if (f[w][2]) vis[j] = true;
          if (f[w][3]) vis[i] = true;
          int sg;
          for (int p = 0; p <= 3; p++)
            if (!vis[p]) {
              sg = p;
              break;
            }
          mov.a[i * 16 + j * 4 + k][j * 16 + k * 4 + sg]++;
        }
  pw[0] = mov;
  for (int i = 1; i <= 29; i++) pw[i] = pw[i - 1] * pw[i - 1];
  int pt = 1;
  cnt[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    res[i].a[63] = 1;
    if (pt > m || inp[pt].x > i)
      res[i] = cal(res[i], a[i]);
    else {
      int r = pt;
      while (r < m && inp[r + 1].x == inp[r].x) r++;
      for (int j = pt; j <= r; j++) {
        res[i] =
            cal(res[i], j == pt ? inp[j].y - 1 : inp[j].y - inp[j - 1].y - 1);
        Vector tmp;
        for (int k = 0; k <= 63; k++) {
          int x = k / 16, y = k / 4 % 4, z = k % 4;
          bool vis[4] = {0};
          if (f[inp[j].c][1]) vis[z] = true;
          if (f[inp[j].c][2]) vis[y] = true;
          if (f[inp[j].c][3]) vis[x] = true;
          int sg;
          for (int p = 0; p <= 3; p++)
            if (!vis[p]) {
              sg = p;
              break;
            }
          tmp.a[y * 16 + z * 4 + sg] =
              (tmp.a[y * 16 + z * 4 + sg] + res[i].a[k]) % Mod;
        }
        res[i] = tmp;
      }
      res[i] = cal(res[i], a[i] - inp[r].y);
      pt = r + 1;
    }
    for (int w = 0; w <= 3; w++)
      for (int x = 0; x <= 3; x++)
        for (int y = 0; y <= 3; y++)
          for (int z = 0; z <= 3; z++)
            cnt[i][w] = (cnt[i][w] + 1ll * cnt[i - 1][w ^ z] *
                                         res[i].a[x * 16 + y * 4 + z]) %
                        Mod;
  }
  cout << cnt[n][0] << endl;
  return 0;
}
