#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0;
  char ch = getchar();
  bool d = 1;
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') d = 0;
  for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
  return d ? x : -x;
}
inline unsigned long long rnd() {
  return ((unsigned long long)rand() << 30 ^ rand()) << 4 | rand() % 4;
}
const int N = 70, M = 1005, mo = 998244353;
struct mat {
  int a[N][N], n, m;
  mat() {}
  mat(int _n, int _m) {
    n = _n;
    m = _m;
    memset(a, 0, sizeof(a));
  }
  int *operator[](int &x) { return a[x]; }
  int *operator[](const int &x) { return a[x]; }
} G, F[30];
mat operator*(mat A, mat B) {
  mat C(A.n, B.m);
  for (int i = (int)(0); i <= (int)(A.n - 1); i++)
    for (int k = (int)(0); k <= (int)(A.m - 1); k++) {
      for (int j = (int)(0); j <= (int)(B.m - 1); j++)
        C[i][j] = (C[i][j] + (long long)A[i][k] * B[k][j]) % mo;
    }
  return C;
}
int co[5], f[5][5], sg[5], sum[N];
bool used[5][5], vis[5];
int MEX(int pos, int co) {
  for (int i = (int)(0); i <= (int)(3); i++) vis[i] = 0;
  for (int i = (int)(1); i <= (int)(min(pos - 1, 3)); i++) {
    if (used[co][i]) vis[sg[pos - i]] = 1;
  }
  for (int i = (int)(0); i <= (int)(3); i++)
    if (!vis[i]) return i;
}
void check(int n) {
  for (int i = (int)(1); i <= (int)(n); i++)
    sg[i] = MEX(i, co[i]), f[i][sg[i]]++;
  int ssw = 0;
  for (int i = (int)(1); i <= (int)(n); i++) ssw = ssw * 4 + sg[i];
  sum[ssw]++;
}
void dfs(int u, int n) {
  if (u == n + 1) {
    check(n);
    return;
  }
  if (co[u] != -1)
    dfs(u + 1, n);
  else {
    for (int i = (int)(1); i <= (int)(3); i++)
      co[u] = i, dfs(u + 1, n), co[u] = -1;
  }
}
int a[1005], ycl[1005][5], b[N], dp[1005][5];
vector<pair<int, int> > v[1005];
int ST(int st, int co) {
  int t = st;
  for (int i = (int)(3); i >= (int)(1); i--) sg[i] = t % 4, t /= 4;
  int ssw = 4 * sg[2] + sg[3], SG = MEX(4, co);
  return ssw * 4 + SG;
}
int main() {
  int n = read();
  for (int i = (int)(1); i <= (int)(n); i++) a[i] = read();
  int m = read();
  for (int i = (int)(1); i <= (int)(m); i++) {
    int x = read(), y = read(), z = read();
    v[x].push_back(make_pair(y, z));
  }
  for (int i = (int)(1); i <= (int)(n); i++) sort(v[i].begin(), v[i].end());
  for (int i = (int)(1); i <= (int)(3); i++) {
    for (int j = (int)(1); j <= (int)(3); j++) used[i][j] = read();
  }
  G.n = G.m = 64;
  for (int i = (int)(0); i <= (int)(63); i++)
    for (int j = (int)(1); j <= (int)(3); j++) G[i][ST(i, j)]++;
  F[0] = G;
  for (int i = (int)(1); i <= (int)(29); i++) F[i] = F[i - 1] * F[i - 1];
  for (int o = (int)(1); o <= (int)(n); o++) {
    if (a[o] <= 3) {
      memset(co, -1, sizeof(co));
      memset(f, 0, sizeof(f));
      for (auto x : v[o]) co[x.first] = x.second;
      dfs(1, a[o]);
      for (int i = (int)(0); i <= (int)(3); i++) ycl[o][i] = f[a[o]][i];
    } else {
      memset(co, -1, sizeof(co));
      memset(sum, 0, sizeof(sum));
      for (auto x : v[o]) {
        if (x.first <= 3)
          co[x.first] = x.second;
        else
          break;
      }
      mat ssw(1, 64);
      dfs(1, 3);
      for (int i = (int)(0); i <= (int)(63); i++) ssw[0][i] = sum[i];
      int pre = 3;
      for (auto x : v[o]) {
        if (x.first <= 3) continue;
        if (x.first - pre > 1) {
          int t = x.first - pre - 1;
          for (int i = (int)(29); i >= (int)(0); i--)
            if (t >> i & 1) ssw = ssw * F[i];
        }
        for (int i = (int)(0); i <= (int)(63); i++) b[i] = 0;
        for (int i = (int)(0); i <= (int)(63); i++) {
          int st = ST(i, x.second);
          b[st] = (b[st] + ssw[0][i]) % mo;
        }
        for (int i = (int)(0); i <= (int)(63); i++) ssw[0][i] = b[i];
        pre = x.first;
      }
      if (pre != a[o]) {
        int t = a[o] - pre;
        for (int i = (int)(29); i >= (int)(0); i--)
          if (t >> i & 1) ssw = ssw * F[i];
      }
      for (int i = (int)(0); i <= (int)(63); i++)
        ycl[o][i % 4] = (ycl[o][i % 4] + ssw[0][i]) % mo;
    }
  }
  dp[0][0] = 1;
  for (int i = (int)(1); i <= (int)(n); i++) {
    for (int j = (int)(0); j <= (int)(3); j++)
      for (int k = (int)(0); k <= (int)(3); k++) {
        dp[i][j] = (dp[i][j] + (long long)dp[i - 1][k] * ycl[i][j ^ k]) % mo;
      }
  }
  cout << dp[n][0];
}
