#include <bits/stdc++.h>
using namespace std;
long long mod = 998244353;
struct vec {
  int n = 64;
  long long v[64];
  vec() {
    for (int i = 0; i < n; i++) v[i] = 0;
  }
};
struct mat {
  int n = 64;
  long long m[64][64];
  mat() {
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++) m[i][j] = 0;
  }
  mat operator+(mat& o) {
    mat r;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++) r.m[i][j] = (m[i][j] + o.m[i][j]) % mod;
    return r;
  }
  mat operator*(mat& o) {
    mat r;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        for (int k = 0; k < n; k++)
          r.m[i][j] = (r.m[i][j] + m[i][k] * o.m[k][j]) % mod;
    return r;
  }
  vec operator*(vec& v) {
    vec r;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++) r.v[i] = (r.v[i] + m[i][j] * v.v[j]) % mod;
    return r;
  }
};
int n, mm, a[2000], f[4][4];
long long w[2000][4], dp[2000][4];
vector<pair<int, int>> c[2000];
vector<mat> mc[4];
mat id;
vec powmat(int i, vec res, long long b) {
  int j = 1;
  while (b > 0) {
    if (b & 1) res = mc[i][j] * res;
    b >>= 1;
    j++;
  }
  return res;
}
int index(int l1, int l2, int l3) { return l1 + (l2 << 2) + (l3 << 4); }
int main() {
  for (int i = 0; i < 64; i++) id.m[i][i] = 1;
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(10);
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];
  cin >> mm;
  int x, y, col;
  for (int i = 0; i < mm; i++)
    cin >> x >> y >> col, x--, y--, col--, c[x].push_back({y, col});
  for (int i = 0; i < n; i++) sort(c[i].begin(), c[i].end());
  for (int i = 0; i < 3; i++)
    for (int j = 0; j < 3; j++) cin >> f[i][j];
  mat sm;
  for (int i = 0; i < 3; i++) {
    mat m;
    for (int l1 = 0; l1 < 4; l1++)
      for (int l2 = 0; l2 < 4; l2++)
        for (int l3 = 0; l3 < 4; l3++) {
          vector<int> nums;
          nums.push_back(-1);
          if (f[i][0]) nums.push_back(l1);
          if (f[i][1]) nums.push_back(l2);
          if (f[i][2]) nums.push_back(l3);
          sort(nums.begin(), nums.end());
          int g = -1;
          for (int i = 0; i < ((int)((nums).size())) - 1; i++)
            if (nums[i + 1] - nums[i] >= 2) {
              g = nums[i] + 1;
              break;
            }
          if (g == -1) g = nums.back() + 1;
          m.m[index(g, l1, l2)][index(l1, l2, l3)] = 1;
        }
    mc[i].push_back(id);
    mc[i].push_back(m);
    sm = sm + m;
  }
  mc[3].push_back(id);
  mc[3].push_back(sm);
  for (int i = 3; i < 4; i++) {
    while ((1LL << (((int)((mc[i]).size())) - 1)) < mod)
      mc[i].push_back(mc[i].back() * mc[i].back());
  }
  for (int i = 0; i < n; i++) {
    vec v;
    v.v[index(3, 3, 3)] = 1;
    int l = -1;
    for (auto p : c[i]) {
      int pos = p.first;
      int col = p.second;
      v = powmat(3, v, pos - l - 1);
      v = mc[col][1] * v;
      l = pos;
    }
    int pos = a[i] - 1;
    v = powmat(3, v, pos - l);
    for (int l1 = 0; l1 < 4; l1++)
      for (int l2 = 0; l2 < 4; l2++)
        for (int l3 = 0; l3 < 4; l3++)
          w[i][l1] = (w[i][l1] + v.v[index(l1, l2, l3)]) % mod;
  }
  for (int i = 0; i < 4; i++) dp[0][i] = w[0][i];
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < 4; j++)
      for (int k = 0; k < 4; k++)
        dp[i][j ^ k] = (dp[i][j ^ k] + dp[i - 1][j] * w[i][k]) % mod;
  }
  cout << dp[n - 1][0] << "\n";
}
