#include <bits/stdc++.h>
using namespace std;
const int N = 1002;
const int M = 64;
const int p = 998244353;
int n, a[N], m;
int x[N], y[N], c[N];
int num[N];
void Add(int &x, int y) {
  x += y;
  while (x >= p) x -= p;
}
struct data {
  int a[M][M];
  data() { memset(a, 0, sizeof a); }
  data operator+(const data &q) const {
    data ans;
    for (int i = (0); i < (M); i++)
      for (int j = (0); j < (M); j++) ans.a[i][j] = (a[i][j] + q.a[i][j]) % p;
    return ans;
  }
  data operator*(const data &q) const {
    data ans;
    for (int i = (0); i < (M); i++)
      for (int k = (0); k < (M); k++)
        for (int j = (0); j < (M); j++)
          Add(ans.a[i][j], 1LL * a[i][k] * q.a[k][j] % p);
    return ans;
  }
} f[4];
data d[30];
inline void read(int &x) {
  char ch = getchar();
  x = 0;
  int w = 0;
  for (; ch < '0' || ch > '9'; ch = getchar())
    if (ch == '-') w = 1;
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    x = (x << 3) + (x << 1) + ch - '0';
  if (w) x = -x;
}
void chkmax(int &x, int y) {
  if (y > x) x = y;
}
int id[4][4][4];
int b[4];
void init() {
  int tot = 0;
  for (int i = (0); i <= (3); i++)
    for (int j = (0); j <= (3); j++)
      for (int k = (0); k <= (3); k++) id[i][j][k] = tot++;
  for (int o = (1); o <= (3); o++) {
    int q, w, e;
    read(q), read(w), read(e);
    for (int i = (0); i <= (3); i++)
      for (int j = (0); j <= (3); j++)
        for (int k = (0); k <= (3); k++) {
          memset(b, 0, sizeof b);
          if (q) b[i] = 1;
          if (w) b[j] = 1;
          if (e) b[k] = 1;
          int xx;
          for (xx = 0; xx <= 4; xx++)
            if (!b[xx]) break;
          f[o].a[id[xx][i][j]][id[i][j][k]] = 1;
        }
  }
  data sum = f[1] + f[2] + f[3];
  for (int i = 0; i < 30; i++) d[i] = sum, sum = sum * sum;
}
int cmp(const int &q, const int &w) {
  if (x[q] != x[w]) return x[q] < x[w];
  return y[q] < y[w];
}
int sum[M], ls[M];
void mul(data &q) {
  for (int i = (0); i < (M); i++) {
    ls[i] = 0;
    for (int j = (0); j < (M); j++) Add(ls[i], 1LL * q.a[i][j] * sum[j] % p);
  }
  for (int i = (0); i < (M); i++) sum[i] = ls[i];
}
void mul(int n) {
  for (int i = 0; n; n >>= 1, i++)
    if (n & 1) mul(d[i]);
}
int s[4], dp[N][4];
int main() {
  read(n);
  for (int i = (1); i <= (n); i++) read(a[i]);
  read(m);
  for (int i = (1); i <= (m); i++)
    read(x[i]), read(y[i]), read(c[i]), num[i] = i;
  init();
  sort(num + 1, num + 1 + m, cmp);
  int w = 1;
  dp[0][0] = 1;
  for (int i = (1); i <= (n); i++) {
    int mx = 0;
    memset(sum, 0, sizeof sum);
    sum[id[3][3][3]] = 1;
    for (; x[num[w]] == i; w++) {
      int xx = y[num[w]];
      mul(xx - mx - 1);
      mul(f[c[num[w]]]);
      mx = xx;
    }
    mul(a[i] - mx);
    memset(s, 0, sizeof s);
    for (int q = (0); q <= (3); q++)
      for (int w = (0); w <= (3); w++)
        for (int e = (0); e <= (3); e++) Add(s[q], sum[id[q][w][e]]);
    for (int j = (0); j <= (3); j++)
      for (int k = (0); k <= (3); k++)
        Add(dp[i][j ^ k], 1LL * dp[i - 1][j] * s[k] % p);
  }
  printf("%d\n", dp[n][0]);
  return 0;
}
