#include <bits/stdc++.h>
using namespace std;
struct mat {
  int a[64][64];
  mat() {}
  mat(int t) {
    for (int i = 0; i < 64; ++i)
      for (int j = 0; j < 64; ++j) a[i][j] = i == j ? t : 0;
  }
  inline friend mat operator+(const mat& a, const mat& b) {
    mat c;
    for (int i = 0; i < 64; ++i)
      for (int j = 0; j < 64; ++j)
        c.a[i][j] = (a.a[i][j] + b.a[i][j]) % 998244353;
    return c;
  }
  inline friend mat operator*(const mat& a, const mat& b) {
    mat c(0);
    for (int i = 0; i < 64; ++i)
      for (int j = 0; j < 64; ++j)
        for (int k = 0; k < 64; ++k)
          c.a[i][j] =
              (c.a[i][j] + (long long)a.a[i][k] * b.a[k][j]) % 998244353;
    return c;
  }
  inline friend mat operator^(mat a, int b) {
    mat c(1);
    for (; b; b >>= 1, a = a * a)
      if (b) c = c * a;
    return c;
  }
};
struct vec {
  int a[64];
  vec() {
    for (int i = 0; i < 64; ++i) a[i] = 0;
  }
  inline friend vec operator*(const mat& a, const vec& b) {
    vec c;
    for (int i = 0; i < 64; ++i)
      for (int j = 0; j < 64; ++j)
        c.a[i] = (c.a[i] + (long long)a.a[i][j] * b.a[j]) % 998244353;
    return c;
  }
};
int n, a[1005], m;
vector<std::pair<int, int> > v[1005];
int f[4];
mat A[4];
mat second[30];
inline void trans(vec& a, const int& b) {
  for (int j = 0; j <= 29; ++j)
    if (b >> j & 1) a = second[j] * a;
}
int fin[1005][4];
int ans[4], tmp[4];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= (n); ++i) scanf("%d", a + i);
  scanf("%d", &m);
  for (int i = 1; i <= (m); ++i) {
    int x, y, c;
    scanf("%d%d%d", &x, &y, &c);
    v[x].push_back(std::make_pair(y, c));
  }
  for (int i = 1; i <= (n); ++i) sort(v[i].begin(), v[i].end());
  for (int i = 1; i <= (3); ++i) {
    for (int j = 1; j <= (3); ++j) scanf("%d", &f[j]);
    for (int s = 0; s < 64; ++s) {
      int s1 = s & 3, s2 = s >> 2 & 3, s3 = s >> 4 & 3, v[4], k = 0;
      for (int j = 0; j <= (3); ++j) v[j] = 0;
      if (f[1]) v[s1] = 1;
      if (f[2]) v[s2] = 1;
      if (f[3]) v[s3] = 1;
      while (v[k]) ++k;
      int ns = (s2 << 4) | (s1 << 2) | k;
      A[i].a[ns][s] = 1;
    }
  }
  second[0] = A[1] + A[2] + A[3];
  for (int i = 1; i <= (29); ++i) second[i] = second[i - 1] * second[i - 1];
  ans[0] = 1;
  for (int i = 1; i <= (n); ++i) {
    vec vi;
    vi.a[63] = 1;
    int lst = 0;
    for (auto e : v[i]) {
      int pos = e.first, col = e.second;
      trans(vi, pos - lst - 1);
      if (col == 1) vi = A[1] * vi;
      if (col == 2) vi = A[2] * vi;
      if (col == 3) vi = A[3] * vi;
      lst = pos;
    }
    trans(vi, a[i] - lst);
    for (int s = 0; s < 64; ++s)
      fin[i][s & 3] = (fin[i][s & 3] + vi.a[s]) % 998244353;
    std::swap(ans, tmp);
    for (int j = 0; j <= (3); ++j) {
      ans[j] = 0;
      for (int k = 0; k <= (3); ++k)
        ans[j] = (ans[j] + (long long)tmp[k] * fin[i][j ^ k]) % 998244353;
    }
  }
  printf("%d\n", ans[0]);
  return 0;
}
