#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int n, arr[N], m, x, y, c;
int cur[N][4];
vector<pair<int, int> > v[N];
const int all = 64;
int num[64][4];
bool f[4][4];
int dp[N][4];
vector<int> good;
vector<vector<int> > mat[N];
int play(vector<int> v, int col) {
  v.push_back(col);
  bool ok[4] = {0};
  for (int i = 0; i < (int)v.size() - 1; i++) {
    if (f[col][(int)v.size() - 1 - i] == 0) continue;
    ok[v[i]] = true;
  }
  v.back() = 0;
  while (ok[v.back()]) v.back()++;
  int po = 1;
  int res = 0;
  for (int i = 1; i < (int)v.size(); i++) {
    res += po * v[i];
    po *= 4;
  }
  return res;
}
vector<int> v2, v3, tmp;
vector<vector<int> > res;
void make() {
  v3.clear();
  v3.resize((int)v2.size(), 0);
  for (int i = 0; i < (int)v2.size(); i++) {
    bool ok[4] = {0};
    for (int j = 0; j < i; j++) {
      if (f[v2[i]][i - j] == 0) continue;
      ok[v3[j]] = true;
    }
    v3[i] = 0;
    while (ok[v3[i]]) v3[i]++;
  }
  res.push_back(v3);
}
void solve(int i) {
  if (i == (int)v2.size()) {
    make();
    return;
  }
  if (v2[i] != -1) {
    solve(i + 1);
    return;
  }
  for (int j = 1; j <= 3; j++) {
    v2[i] = j;
    solve(i + 1);
    v2[i] = -1;
  }
}
vector<int> tmp2;
vector<vector<int> > mul(vector<vector<int> > &a, vector<vector<int> > &b) {
  vector<vector<int> > res((int)a.size(), vector<int>((int)b[0].size(), 0));
  for (int i = 0; i < (int)a.size(); i++) {
    for (int j = 0; j < (int)b[0].size(); j++) {
      for (int k = 0; k < (int)a[0].size(); k++) {
        res[i][j] += (long long)a[i][k] * b[k][j] % 998244353;
        if (res[i][j] >= 998244353) res[i][j] -= 998244353;
      }
    }
  }
  return res;
}
void mul2(vector<int> &a, int idx) {
  tmp2.clear();
  tmp2.resize(all, 0);
  for (int i = 0; i < all; i++) {
    for (int j = 0; j < all; j++) {
      tmp2[j] += (long long)mat[idx][i][j] * a[i] % 998244353;
      if (tmp2[j] >= 998244353) tmp2[j] -= 998244353;
    }
  }
  for (int i = 0; i < all; i++) a[i] = tmp2[i];
}
void init() {
  mat[0].clear();
  mat[0].resize(all, vector<int>(all, 0));
  for (int i = 0; i < all; i++) {
    for (int k = 1; k <= 3; k++) {
      mat[0][i][num[i][k]]++;
    }
  }
  for (int j = 1; j < 30; j++) {
    mat[j] = mul(mat[j - 1], mat[j - 1]);
  }
}
void make(vector<int> &k, int len) {
  if (len == 0) return;
  for (int j = 29; j >= 0; j--) {
    if (((len >> j) & 1) == 1) {
      mul2(k, j);
    }
  }
}
void make2(vector<int> &k, int col) {
  tmp.clear();
  tmp.resize((int)k.size(), 0);
  for (int i = 0; i < all; i++) {
    for (int j = 1; j <= 3; j++) {
      if (j != col) continue;
      tmp[num[i][j]] += k[i];
      if (tmp[num[i][j]] >= 998244353) tmp[num[i][j]] -= 998244353;
    }
  }
  for (int i = 0; i < all; i++) k[i] = tmp[i];
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", &arr[i]);
  scanf("%d", &m);
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &x, &y, &c);
    x--;
    y--;
    v[x].push_back(make_pair(y, c));
  }
  for (int i = 1; i <= 3; i++) {
    for (int j = 1; j <= 3; j++) {
      scanf("%d", &f[i][j]);
    }
  }
  vector<int> tmp(3);
  for (int i = 0; i < 4 * 4 * 4; i++) {
    tmp[0] = i % 4;
    tmp[1] = (i % 16) / 4;
    tmp[2] = i / 16;
    for (int j = 1; j <= 3; j++) {
      num[i][j] = play(tmp, j);
    }
  }
  init();
  vector<int> k(all, 0);
  for (int i = 0; i < n; i++) {
    v2.clear();
    v2.resize(min(3, arr[i]), -1);
    res.clear();
    sort(v[i].begin(), v[i].end());
    for (int j = 0; j < (int)v[i].size(); j++) {
      if (v[i][j].first >= 3) continue;
      v2[v[i][j].first] = v[i][j].second;
    }
    solve(0);
    if (arr[i] <= 3) {
      for (int j = 0; j < (int)res.size(); j++) {
        cur[i][res[j].back()]++;
      }
      continue;
    }
    k.clear();
    k.resize(all, 0);
    for (int j = 0; j < (int)res.size(); j++) {
      k[res[j][0] + res[j][1] * 4 + res[j][2] * 16]++;
    }
    int last = 2;
    for (int j = 0; j < (int)v[i].size(); j++) {
      if (v[i][j].first <= last) continue;
      make(k, v[i][j].first - last - 1);
      make2(k, v[i][j].second);
      last = v[i][j].first;
    }
    make(k, arr[i] - 1 - last);
    for (int j = 0; j < all; j++) {
      cur[i][j / 16] += k[j];
      if (cur[i][j / 16] >= 998244353) cur[i][j / 16] -= 998244353;
    }
  }
  dp[n][0] = 1;
  for (int i = n - 1; i >= 0; i--) {
    for (int j = 0; j < 4; j++) {
      dp[i][j] = 0;
      for (int k = 0; k < 4; k++) {
        dp[i][j] += (long long)dp[i + 1][(j ^ k)] * cur[i][k] % 998244353;
        if (dp[i][j] >= 998244353) dp[i][j] -= 998244353;
      }
    }
  }
  cout << dp[0][0] << endl;
  return 0;
}
