#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
template <int maxn>
struct Vector {
  long long int entry[maxn];
  Vector() { memset(entry, 0, sizeof entry); }
};
template <int maxn>
struct Matrix {
  long long int entry[maxn][maxn];
  Matrix() { memset(entry, 0, sizeof entry); }
  Matrix<maxn> operator*(const Matrix<maxn> &rhs) const {
    Matrix<maxn> res;
    for (int i = 0; i < maxn; i++)
      for (int j = 0; j < maxn; j++)
        for (int k = 0; k < maxn; k++)
          res.entry[i][j] =
              (res.entry[i][j] + entry[i][k] * rhs.entry[k][j]) % mod;
    return res;
  }
  Matrix<maxn> operator+(const Matrix<maxn> &rhs) const {
    Matrix<maxn> res;
    for (int i = 0; i < maxn; i++)
      for (int j = 0; j < maxn; j++)
        res.entry[i][j] = (entry[i][j] + rhs.entry[i][j]) % mod;
    return res;
  }
  Vector<maxn> operator*(const Vector<maxn> &rhs) const {
    Vector<maxn> res;
    for (int i = 0; i < maxn; i++)
      for (int j = 0; j < maxn; j++)
        res.entry[i] = (res.entry[i] + entry[i][j] * rhs.entry[j]) % mod;
    return res;
  }
};
int a[1001];
Matrix<64> ex[31];
Matrix<64> color_trans[4];
vector<pair<int, int> > color[1001];
int f[4][4];
int mex(int mask) {
  bool on[4] = {0, 0, 0, 0};
  on[3 & mask] = true;
  on[3 & (mask >> 2)] = true;
  on[3 & (mask >> 4)] = true;
  for (int i = 0;; i++)
    if (!on[i]) return i;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  cin >> m;
  for (int i = 1, x, p, c; i <= m; i++) {
    cin >> x >> p >> c;
    color[x].emplace_back(make_pair(p, c));
  }
  for (int i = 1; i <= 3; i++)
    for (int j = 1; j <= 3; j++) cin >> f[i][j];
  for (int c = 1; c <= 3; c++) {
    int mask = 63 - (f[c][1] * 48 + f[c][2] * 12 + f[c][3] * 3);
    for (int i = 0; i < 64; i++) {
      int pos = (mex(i | mask) << 4) | (i >> 2);
      color_trans[c].entry[pos][i] = 1;
    }
  }
  ex[0] = color_trans[1] + color_trans[2] + color_trans[3];
  for (int i = 1; i <= 30; i++) ex[i] = ex[i - 1] * ex[i - 1];
  for (int i = 1; i <= n; i++) sort(color[i].begin(), color[i].end());
  vector<long long int> sg_dp_pre(4), sg_dp(4);
  sg_dp[0] = 1;
  for (int i = 1; i <= n; i++) {
    Vector<64> v;
    v.entry[63] = 1;
    sg_dp_pre = sg_dp;
    for (auto &it : sg_dp) it = 0;
    int pre = 0;
    for (auto it : color[i]) {
      int diff = it.first - pre - 1;
      for (int j = 0; j <= 30; j++)
        if (diff & (1 << j)) v = ex[j] * v;
      v = color_trans[it.second] * v;
      pre = it.first;
    }
    int diff = a[i] - pre;
    for (int j = 0; j <= 30; j++)
      if (diff & (1 << j)) v = ex[j] * v;
    for (int k = 0; k < 4; k++)
      for (int j = 0; j < 64; j++)
        sg_dp[(j >> 4) ^ k] =
            (sg_dp[(j >> 4) ^ k] + v.entry[j] * sg_dp_pre[k]) % mod;
  }
  cout << sg_dp[0] << endl;
}
