#include <bits/stdc++.h>
int xx[1000], yy[1000], cc[1000];
int pp[30][((3 + 1) * (3 + 1) * (3 + 1))][((3 + 1) * (3 + 1) * (3 + 1))];
void mult(int aa[][((3 + 1) * (3 + 1) * (3 + 1))],
          int bb[][((3 + 1) * (3 + 1) * (3 + 1))],
          int cc[][((3 + 1) * (3 + 1) * (3 + 1))]) {
  int i, j, k;
  for (i = 0; i < ((3 + 1) * (3 + 1) * (3 + 1)); i++) {
    int *ai = aa[i], *ci = cc[i];
    for (j = 0; j < ((3 + 1) * (3 + 1) * (3 + 1)); j++) cc[i][j] = 0;
    for (k = 0; k < ((3 + 1) * (3 + 1) * (3 + 1)); k++) {
      int *bk = bb[k], aik = ai[k], bkj;
      if (aik > 0)
        for (j = 0; j < ((3 + 1) * (3 + 1) * (3 + 1)); j++) {
          bkj = bk[j];
          if (bkj > 0 &&
              (ci[j] += (long long)aik * bkj % 998244353) >= 998244353)
            ci[j] -= 998244353;
        }
    }
  }
}
void mult1(int *aa, int bb[][((3 + 1) * (3 + 1) * (3 + 1))]) {
  static int cc[((3 + 1) * (3 + 1) * (3 + 1))];
  int i, j, x;
  for (j = 0; j < ((3 + 1) * (3 + 1) * (3 + 1)); j++) {
    x = 0;
    for (i = 0; i < ((3 + 1) * (3 + 1) * (3 + 1)); i++)
      x = (x + (long long)aa[i] * bb[i][j]) % 998244353;
    cc[j] = x;
  }
  for (j = 0; j < ((3 + 1) * (3 + 1) * (3 + 1)); j++) aa[j] = cc[j];
}
void mult_(int *aa, int k) {
  int h;
  for (h = 0; k; h++, k >>= 1)
    if (k & 1) mult1(aa, pp[h]);
}
void init(
    int ff[][3], int aa[][((3 + 1) * (3 + 1) * (3 + 1))],
    int aa_[][((3 + 1) * (3 + 1) * (3 + 1))][((3 + 1) * (3 + 1) * (3 + 1))]) {
  int h, i, j, c;
  for (i = 0; i < ((3 + 1) * (3 + 1) * (3 + 1)); i++) {
    int a2 = i / (3 + 1) / (3 + 1), a1 = i / (3 + 1) % (3 + 1),
        a0 = i % (3 + 1);
    for (c = 0; c < 3; c++) {
      int x = 0, a_;
      if (ff[c][0]) x |= 1 << a0;
      if (ff[c][1]) x |= 1 << a1;
      if (ff[c][2]) x |= 1 << a2;
      for (a_ = 0; x & 1 << a_; a_++)
        ;
      j = ((a1 * (3 + 1) + a0) * (3 + 1) + a_);
      aa[i][j] += aa_[c][i][j] = 1;
    }
  }
  for (i = 0; i < ((3 + 1) * (3 + 1) * (3 + 1)); i++)
    for (j = 0; j < ((3 + 1) * (3 + 1) * (3 + 1)); j++) pp[0][i][j] = aa[i][j];
  for (h = 0; h + 1 < 30; h++) mult(pp[h], pp[h], pp[h + 1]);
}
int compare(const void *a, const void *b) {
  int i = *(int *)a;
  int j = *(int *)b;
  return xx[i] != xx[j] ? xx[i] - xx[j] : yy[i] - yy[j];
}
int main() {
  static int ll[1000], hh[1000], ff[3][3],
      aa[((3 + 1) * (3 + 1) * (3 + 1))][((3 + 1) * (3 + 1) * (3 + 1))],
      aa_[3][((3 + 1) * (3 + 1) * (3 + 1))][((3 + 1) * (3 + 1) * (3 + 1))],
      vv[((3 + 1) * (3 + 1) * (3 + 1))], kk[3 + 1], dp[3 + 1], dq[3 + 1];
  int n, m, h, i, j, x, y, y_;
  scanf("%d", &n);
  for (x = 0; x < n; x++) scanf("%d", &ll[x]);
  scanf("%d", &m);
  for (h = 0; h < m; h++) {
    scanf("%d%d%d", &xx[h], &yy[h], &cc[h]), xx[h]--, yy[h]--, cc[h]--;
    hh[h] = h;
  }
  for (i = 0; i < 3; i++)
    for (j = 0; j < 3; j++) scanf("%d", &ff[i][j]);
  init(ff, aa, aa_);
  qsort(hh, m, sizeof *hh, compare);
  dp[0] = 1;
  for (x = 0, h = 0; x < n; x++) {
    memset(vv, 0, sizeof vv);
    vv[(3 * (3 + 1) + 3) * (3 + 1) + 3] = 1;
    y = -1;
    while (h < m && xx[hh[h]] == x) {
      y_ = yy[hh[h]];
      if (y != y_) {
        mult_(vv, y_ - y - 1);
        mult1(vv, aa_[cc[hh[h]]]);
      }
      y = y_;
      h++;
    }
    y_ = ll[x] - 1;
    if (y != y_) mult_(vv, y_ - y);
    y = y_;
    memset(kk, 0, sizeof kk);
    for (i = 0; i < ((3 + 1) * (3 + 1) * (3 + 1)); i++)
      kk[i % (3 + 1)] = (kk[i % (3 + 1)] + vv[i]) % 998244353;
    memset(dq, 0, sizeof dq);
    for (i = 0; i <= 3; i++)
      for (j = 0; j <= 3; j++)
        dq[i ^ j] = (dq[i ^ j] + (long long)dp[i] * kk[j]) % 998244353;
    for (i = 0; i <= 3; i++) dp[i] = dq[i];
  }
  printf("%d\n", dp[0]);
  return 0;
}
