#include <bits/stdc++.h>
using namespace std;
using namespace std;
const int MOD = 998244353;
int add(int x, int y) { return (x + y) % MOD; }
int mul(int x, int y) { return (x * 1ll * y) % MOD; }
vector<int> mul(const vector<vector<int>> &a, const vector<int> &b) {
  int n = a.size();
  int m = b.size();
  vector<int> c(m);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) c[i] = add(c[i], mul(b[j], a[i][j]));
  return c;
}
vector<vector<int>> add(const vector<vector<int>> &a,
                        const vector<vector<int>> &b) {
  int n = a.size();
  int m = a[0].size();
  vector<vector<int>> c(n, vector<int>(m, 0));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) c[i][j] = add(a[i][j], b[i][j]);
  return c;
}
void iadd(vector<vector<int>> &a, const vector<vector<int>> &b) {
  int n = a.size();
  int m = a[0].size();
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) a[i][j] = add(a[i][j], b[i][j]);
  return;
}
vector<vector<int>> mul(const vector<vector<int>> &a,
                        const vector<vector<int>> &b) {
  int x = a.size();
  int y = b.size();
  int z = b[0].size();
  vector<vector<int>> c(x, vector<int>(z, 0));
  for (int i = 0; i < x; i++)
    for (int j = 0; j < y; j++)
      for (int k = 0; k < z; k++) c[i][k] = add(c[i][k], mul(a[i][j], b[j][k]));
  return c;
}
vector<vector<int>> binpow(vector<vector<int>> a, int d) {
  int n = a.size();
  vector<vector<int>> c = vector<vector<int>>(n, vector<int>(n, 0));
  for (int i = 0; i < n; i++) c[i][i] = 1;
  while (d > 0) {
    if (d % 2 == 1) c = mul(c, a);
    a = mul(a, a);
    d /= 2;
  }
  return c;
}
vector<vector<int>> I(int t) {
  vector<vector<int>> c(t, vector<int>(t, 0));
  for (int i = 0; i < t; i++) c[i][i] = 1;
  return c;
}
int len[1005];
vector<pair<int, int>> colored[1005];
int f[3][3];
vector<vector<int>> color_matrix[3], full_matrix;
bool mex[4];
void build_matrix(int color) {
  auto &matrix = color_matrix[color];
  matrix.resize(64, vector<int>(64, 0));
  for (int i = 0; i < 64; i++) {
    int s3 = (i >> 4) & 3, s2 = (i >> 2) & 3, s1 = i & 3;
    memset(mex, 0, sizeof(mex));
    if (f[color][0]) mex[s1] = 1;
    if (f[color][1]) mex[s2] = 1;
    if (f[color][2]) mex[s3] = 1;
    s3 = s2, s2 = s1;
    if (!mex[0])
      s1 = 0;
    else if (!mex[1])
      s1 = 1;
    else if (!mex[2])
      s1 = 2;
    else
      s1 = 3;
    int j = (s3 << 4) | (s2 << 2) | s1;
    matrix[j][i]++;
  }
}
vector<vector<int>> pre[40];
long long dp[1005][5];
void init_full() {
  pre[0] = full_matrix;
  for (int i = 1; i <= 30; i++) {
    pre[i] = mul(pre[i - 1], pre[i - 1]);
  }
}
void powmul(int d, vector<int> &b) {
  for (int i = 0; i <= 30; i++) {
    if (d & 1) b = mul(pre[i], b);
    d >>= 1;
  }
  return;
}
void build_matrixs() {
  for (int i = 0; i < 3; i++) {
    build_matrix(i);
  }
  full_matrix.resize(64, vector<int>(64, 0));
  iadd(full_matrix, color_matrix[0]);
  iadd(full_matrix, color_matrix[1]);
  iadd(full_matrix, color_matrix[2]);
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &len[i]);
  }
  int m;
  scanf("%d", &m);
  for (int i = 1; i <= m; i++) {
    int x, y, c;
    scanf("%d%d%d", &x, &y, &c);
    colored[x - 1].push_back({y - 1, c - 1});
  }
  for (int i = 0; i < n; i++) {
    sort(colored[i].begin(), colored[i].end());
  }
  for (int i = 0; i < 3; i++)
    for (int j = 0; j < 3; j++) {
      scanf("%d", &f[i][j]);
    }
  build_matrixs();
  init_full();
  dp[0][0] = 1;
  for (int i = 0; i < n; i++) {
    vector<int> ret(64, 0);
    ret[63] = 1;
    int lst = 0;
    for (auto col : colored[i]) {
      powmul(col.first - lst, ret);
      ret = mul(color_matrix[col.second], ret);
      lst = col.first + 1;
    }
    powmul(len[i] - lst, ret);
    for (int k = 0; k <= 3; k++)
      for (int j = 0; j < 64; j++) {
        (dp[i + 1][k ^ (j & 3)] += (long long)dp[i][k] * ret[j] % MOD) %= MOD;
      }
  }
  printf("%lld\n", dp[n][0]);
}
