#include <bits/stdc++.h>
using namespace std;
struct M {
  int a[64][64];
  int r, c;
  M(int _r = 0, int _c = 0) {
    r = _r;
    c = _c;
    memset(a, 0, sizeof(a));
  }
  void init(int x) {
    r = c = x;
    memset(a, 0, sizeof(a));
    for (int i = 0; i < x; i++) a[i][i] = 1;
  }
};
M operator*(const M& a, const M& b) {
  M ans(a.r, b.c);
  for (int i = 0; i < ans.r; i++)
    for (int j = 0; j < ans.c; j++)
      for (int k = 0; k < a.c; k++)
        ans.a[i][j] =
            (ans.a[i][j] + (long long)a.a[i][k] * b.a[k][j]) % 998244353;
  return ans;
}
int f[4][4];
bool vis[4];
M T[31];
int id(int a, int b, int c) { return a + b * 4 + c * 16; }
int to(int a, int b, int c, int d) {
  memset(vis, 0, sizeof(vis));
  if (f[d][1]) vis[c] = true;
  if (f[d][2]) vis[b] = true;
  if (f[d][3]) vis[a] = true;
  int ans = 0;
  while (vis[ans]) ans++;
  return ans;
}
void calc() {
  T[0] = M(64, 64);
  for (int a = 0; a < 4; a++)
    for (int b = 0; b < 4; b++)
      for (int c = 0; c < 4; c++)
        for (int d = 1; d <= 3; d++)
          T[0].a[id(a, b, c)][id(b, c, to(a, b, c, d))]++;
  for (int i = 1; i <= 30; i++) T[i] = T[i - 1] * T[i - 1];
  return;
}
M fpow(M ans, int b) {
  int now = 0;
  while (b) {
    if (b & 1) ans = ans * T[now];
    b >>= 1;
    now++;
  }
  return ans;
}
vector<pair<int, int> > pos[1010];
int a[1010];
int num[1010][4];
void solve(int x) {
  M s(1, 64);
  s.a[0][id(3, 3, 3)] = 1;
  for (int i = 1; i < pos[x].size(); i++) {
    s = fpow(s, pos[x][i].first - pos[x][i - 1].first - 1);
    M t(1, 64);
    for (int j = 0; j < 64; j++) {
      int a = j % 4, b = (j / 4) % 4, c = j / 16;
      t.a[0][id(b, c, to(a, b, c, pos[x][i].second))] += s.a[0][id(a, b, c)];
      t.a[0][id(b, c, to(a, b, c, pos[x][i].second))] %= 998244353;
    }
    s = t;
  }
  s = fpow(s, a[x] - pos[x][pos[x].size() - 1].first);
  for (int i = 0; i < 64; i++)
    num[x][i / 16] = (num[x][i / 16] + s.a[0][i]) % 998244353;
  return;
}
int dp[1010][4];
int main() {
  int n, m;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  scanf("%d", &m);
  for (int i = 1; i <= m; i++) {
    int x, y, c;
    scanf("%d %d %d", &x, &y, &c);
    pos[x].push_back(make_pair(y, c));
  }
  for (int i = 1; i <= n; i++) pos[i].push_back(make_pair(0, 0));
  for (int i = 1; i <= n; i++) sort(pos[i].begin(), pos[i].end());
  for (int i = 1; i <= 3; i++)
    for (int j = 1; j <= 3; j++) scanf("%d", &f[i][j]);
  calc();
  dp[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    solve(i);
    for (int j = 0; j < 4; j++)
      for (int k = 0; k < 4; k++)
        dp[i][j ^ k] =
            (dp[i][j ^ k] + (long long)dp[i - 1][j] * num[i][k]) % 998244353;
  }
  printf("%d\n", dp[n][0]);
  return 0;
}
