#include <bits/stdc++.h>
const int md = 998244353;
inline int add(int a, int b) {
  a += b;
  if (a >= md) a -= md;
  return a;
}
inline int mul(int a, int b) { return (long long)a * b % md; }
const int M = 64;
class matrix {
 public:
  int a[M][M];
  int n, m;
  matrix(int n_ = 0, int m_ = 0) {
    memset(*a, 0, 4 * M * M);
    n = n_;
    m = m_;
  }
  matrix operator*(matrix arg) {
    matrix r(n, arg.m);
    for (int i = 0; i < n; ++i)
      for (int k = 0; k < m; ++k)
        for (int j = 0; j < arg.m; ++j)
          r.a[i][j] = add(r.a[i][j], mul(a[i][k], arg.a[k][j]));
    return r;
  }
  void operator*=(matrix arg) { *this = *this * arg; }
};
const int N = 1000;
const int LG = 30;
bool mv[3][3];
int n, m, dp[N + 1][4], amt[N];
std::pair<std::pair<int, int>, int> a[N];
matrix x(64, 64), pws[LG];
void mul_exp_init() {
  pws[0] = matrix(64, 64);
  for (int i = 0; i < 64; ++i)
    for (int j = 0; j < 64; ++j) {
      int f[3], t[3];
      bool ok = true;
      for (int k = 0; k < 3; ++k) {
        f[k] = (i >> k * 2) & 3;
        t[k] = (j >> k * 2) & 3;
        if (k && t[k] != f[k - 1]) ok = false;
      }
      if (ok) {
        for (int k = 0; k < 3; ++k) {
          bool u[4] = {0};
          for (int l = 0; l < 3; ++l)
            if (mv[k][l]) u[f[l]] = true;
          int ex;
          for (ex = 0; u[ex]; ++ex)
            ;
          if (ex == t[0]) ++pws[0].a[i][j];
        }
      }
    }
  for (int i = 1; i < LG; ++i) pws[i] = pws[i - 1] * pws[i - 1];
}
void mul_exp(matrix &a, int b) {
  for (int i = 0; b; ++i, b >>= 1)
    if (b & 1) a *= pws[i];
}
void single_color(matrix &a, int c) {
  matrix r(1, 64);
  for (int i = 0; i < 64; ++i) {
    bool u[4] = {0};
    int f[3];
    for (int j = 0; j < 3; ++j) f[j] = (i >> j * 2) & 3;
    for (int j = 0; j < 3; ++j)
      if (mv[c][j]) u[f[j]] = true;
    int ex;
    for (ex = 0; u[ex]; ++ex)
      ;
    for (int j = 2; j; --j) f[j] = f[j - 1];
    f[0] = ex;
    int res = 0;
    for (int j = 0; j < 3; ++j) res |= f[j] << j * 2;
    r.a[0][res] = add(r.a[0][res], a.a[0][i]);
  }
  a = r;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) scanf("%d", amt + i);
  scanf("%d", &m);
  for (int i = 0; i < m; ++i) {
    scanf("%d%d%d", &a[i].first.first, &a[i].first.second, &a[i].second);
    --a[i].first.first;
    --a[i].second;
  }
  for (int i = 0; i < 3; ++i)
    for (int j = 0; j < 3; ++j) {
      int v;
      scanf("%d", &v);
      mv[i][j] = v;
    }
  mul_exp_init();
  std::sort(a, a + m);
  dp[0][0] = 1;
  for (int i = 0, j = 0; i < n; ++i) {
    matrix cur(1, 64);
    cur.a[0][63] = 1;
    int last = 0;
    while (j < m && a[j].first.first == i) {
      mul_exp(cur, a[j].first.second - last - 1);
      single_color(cur, a[j].second);
      last = a[j].first.second;
      ++j;
    }
    mul_exp(cur, amt[i] - last);
    for (int k = 0; k < 64; ++k)
      for (int l = 0; l < 4; ++l)
        dp[i + 1][k & 3 ^ l] =
            add(dp[i + 1][k & 3 ^ l], mul(dp[i][l], cur.a[0][k]));
  }
  printf("%d\n", dp[n][0]);
  return 0;
}
