#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10;
const int mod = 998244353;
int n, m, x, y, c, a[N], dp[N][4] = {0};
vector<pair<int, int> > pt[N];
int f[3][4];
vector<vector<int> > T[31];
vector<int> int2state(int mask) {
  vector<int> state;
  for (int i = 0; i < 3; i++) {
    state.push_back(mask % 4);
    mask /= 4;
  }
  return state;
}
int state2int(vector<int> state) {
  int integer = 0, pw4 = 1;
  for (int i = 0; i < state.size(); i++) {
    integer += pw4 * state[i];
    pw4 *= 4;
  }
  return integer;
}
int find_grundy(int col, vector<int> p) {
  vector<int> used(4, 0);
  for (int i = 1; i <= 3; i++) {
    if (f[col][i]) {
      used[p[i - 1]] = 1;
    }
  }
  for (int i = 0; i < 3; i++) {
    if (used[i]) continue;
    return i;
  }
  return 3;
}
vector<int> extend_state(int col, vector<int> p) {
  int z = find_grundy(col, p);
  p.insert(p.begin(), z);
  p.pop_back();
  return p;
}
vector<vector<int> > add(vector<vector<int> > &a, vector<vector<int> > &b) {
  vector<vector<int> > c(a.size(), vector<int>(a[0].size(), 0));
  for (int i = 0; i < a.size(); i++) {
    for (int j = 0; j < b.size(); j++) {
      c[i][j] = (a[i][j] + b[i][j]) % mod;
    }
  }
  return c;
}
vector<vector<int> > mul(vector<vector<int> > &a, vector<vector<int> > &b) {
  vector<vector<int> > c(a.size(), vector<int>(b[0].size(), 0));
  for (int i = 0; i < a.size(); i++) {
    for (int j = 0; j < b[0].size(); j++) {
      for (int k = 0; k < b.size(); k++) {
        c[i][j] += 1LL * a[i][k] * b[k][j] % mod;
        c[i][j] %= mod;
      }
    }
  }
  return c;
}
vector<vector<int> > binpw(vector<vector<int> > &a, int pw) {
  for (int i = 0; i <= 30; i++) {
    if (pw & (1 << i)) {
      a = mul(T[i], a);
    }
  }
  return a;
}
int main() {
  vector<vector<int> > color[3];
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  scanf("%d", &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d %d %d", &x, &y, &c);
    pt[x].push_back({y, c - 1});
  }
  for (int i = 1; i <= n; i++) {
    sort(pt[i].begin(), pt[i].end());
  }
  for (int i = 0; i < 3; i++) {
    for (int j = 1; j <= 3; j++) {
      scanf("%d", &f[i][j]);
    }
  }
  for (int i = 0; i < 3; i++) {
    vector<vector<int> > res(64, vector<int>(64, 0));
    for (int j = 0; j < 64; j++) {
      int nxt = state2int(extend_state(i, int2state(j)));
      res[nxt][j]++, res[nxt][j] %= mod;
    }
    color[i] = res;
  }
  T[0] = color[0];
  for (int i = 1; i < 3; i++) {
    T[0] = add(T[0], color[i]);
  }
  for (int i = 1; i <= 30; i++) {
    T[i] = mul(T[i - 1], T[i - 1]);
  }
  dp[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    int prev = 0;
    vector<vector<int> > curr(64, vector<int>(1, 0));
    curr[state2int({3, 3, 3})][0] = 1;
    for (auto x : pt[i]) {
      curr = binpw(curr, x.first - 1 - prev);
      curr = mul(color[x.second], curr);
      prev = x.first;
    }
    curr = binpw(curr, a[i] - prev);
    for (int j = 0; j <= 3; j++) {
      for (int k = 0; k < 64; k++) {
        int ways_for_xor_k = curr[k][0];
        dp[i][j] +=
            1LL * ways_for_xor_k * dp[i - 1][j ^ (int2state(k)[0])] % mod;
        dp[i][j] %= mod;
      }
    }
  }
  printf("%d\n", dp[n][0]);
  return 0;
}
