#include <bits/stdc++.h>
using namespace std;
const int N = (int)2005, mod = (int)998244353;
int m = 64;
vector<pair<int, int>> queries[N];
struct matrix {
  int a[64][64];
  matrix() { memset(a, 0, sizeof a); }
};
struct row {
  int a[64];
  row() { memset(a, 0, sizeof a); }
};
matrix mul(matrix a, matrix b) {
  matrix res;
  for (int i = 0; i < m; ++i)
    for (int k = 0; k < m; ++k)
      if (a.a[i][k])
        for (int j = 0; j < m; ++j)
          if (b.a[k][j])
            res.a[i][j] =
                (res.a[i][j] + (long long)a.a[i][k] * b.a[k][j]) % mod;
  return res;
}
row mul(row a, matrix b) {
  row res;
  for (int i = 0; i < m; ++i)
    for (int j = 0; j < m; ++j)
      res.a[j] = (res.a[j] + a.a[i] * (long long)b.a[i][j]) % mod;
  return res;
}
matrix add(matrix a, matrix b) {
  matrix res;
  for (int i = 0; i < m; ++i)
    for (int j = 0; j < m; ++j) res.a[i][j] = (a.a[i][j] + b.a[i][j]) % mod;
  return res;
}
matrix c[3];
matrix p[30];
row pw(row cur, int first) {
  for (int j = 0; j < 30; ++j)
    if (first >> j & 1) cur = mul(cur, p[j]);
  return cur;
}
int dp[4], odp[4], f[3][3], a[N];
int main() {
  int n;
  cin >> n;
  for (int j = 0; j < n; ++j) {
    cin >> a[j];
  }
  int q;
  cin >> q;
  for (int j = 0; j < q; ++j) {
    int first, second, c;
    cin >> first >> second >> c;
    --first, --c;
    queries[first].push_back(make_pair(second, c));
  }
  for (int j = 0; j < n; ++j) sort((queries[j]).begin(), (queries[j]).end());
  for (int i = 0; i < 3; ++i)
    for (int j = 0; j < 3; ++j) cin >> f[i][j];
  for (int col = 0; col < 3; ++col) {
    for (int start = 0; start < m; ++start) {
      int end = (start * 4) % 64;
      set<int> st;
      for (int j = 0; j < 3; ++j)
        if (f[col][j]) {
          int num = (start >> (j * 2)) & 3;
          st.insert(num);
        }
      int mex = 0;
      while (st.find(mex) != st.end()) ++mex;
      end += mex;
      c[col].a[start][end] = 1;
    }
    p[0] = add(p[0], c[col]);
  }
  for (int j = 1; j < 30; ++j) p[j] = mul(p[j - 1], p[j - 1]);
  dp[0] = 1;
  for (int strip = 0; strip < n; ++strip) {
    row cur;
    cur.a[63] = 1;
    int use = 0;
    for (auto first : queries[strip]) {
      cur = pw(cur, first.first - use - 1);
      cur = mul(cur, c[first.second]);
      use = first.first;
    }
    cur = pw(cur, a[strip] - use);
    memcpy(odp, dp, sizeof dp);
    memset(dp, 0, sizeof dp);
    for (int cur_use = 0; cur_use < 4; ++cur_use) {
      int sum = 0;
      for (int s = cur_use; s < m; s += 4) sum = (sum + cur.a[s]) % mod;
      for (int last = 0; last < 4; ++last) {
        dp[last ^ cur_use] =
            (dp[last ^ cur_use] + (long long)sum * odp[last]) % mod;
      }
    }
  }
  cout << dp[0] << '\n';
}
