#include <bits/stdc++.h>
using namespace std;
long long tai_sao_toi_lai_rte_dit_cac[1005][5];
long long n, dora;
long long m[66][66][35];
pair<long long, long long> lim[1005][4];
long long dp[65];
long long mod = 998244353;
long long a[1005];
vector<pair<long long, long long> > que[1005];
long long ans[1005][5];
long long f[5][5];
long long gmex(vector<long long> aa) {
  long long cnt[5];
  memset(cnt, 0, sizeof(cnt));
  for (auto i : aa) {
    cnt[i]++;
  }
  for (long long i = 0; i < 4; i++) {
    if (!cnt[i]) {
      return i;
    }
  }
}
long long ghash(long long i, long long j, long long k) {
  return i + j * 4 + k * 16;
}
void init() {
  for (long long i = 0; i <= 3; i++) {
    for (long long j = 0; j <= 3; j++) {
      for (long long k = 0; k <= 3; k++) {
        for (long long d = 1; d <= 3; d++) {
          vector<long long> cac = {};
          if (f[d][1]) cac.push_back(i);
          if (f[d][2]) cac.push_back(j);
          if (f[d][3]) cac.push_back(k);
          m[ghash(gmex(cac), i, j)][ghash(i, j, k)][0]++;
        }
      }
    }
  }
  for (long long bac = 1; bac <= 30; bac++) {
    for (long long i = 0; i < 64; i++) {
      for (long long j = 0; j < 64; j++) {
        for (long long k = 0; k < 64; k++) {
          m[i][j][bac] += m[i][k][bac - 1] * m[k][j][bac - 1];
          m[i][j][bac] %= mod;
        }
      }
    }
  }
}
void Dora(pair<long long, long long> col, long long pos) {
  long long dp1[65];
  memset(dp1, 0, sizeof(dp1));
  for (long long i = 0; i <= 3; i++) {
    for (long long j = 0; j <= 3; j++) {
      for (long long k = 0; k <= 3; k++) {
        for (long long d = col.first; d <= col.second; d++) {
          vector<long long> cac = {};
          if (f[d][1] && pos > 1) cac.push_back(i);
          if (f[d][2] && pos > 2) cac.push_back(j);
          if (f[d][3] && pos > 3) cac.push_back(k);
          dp1[ghash(gmex(cac), i, j)] += dp[ghash(i, j, k)];
          dp1[ghash(gmex(cac), i, j)] %= mod;
        }
      }
    }
  }
  for (long long i = 0; i < 64; i++) {
    dp[i] = dp1[i];
  }
}
void dangyeu(long long bac) {
  long long dp1[65];
  memset(dp1, 0, sizeof(dp1));
  for (long long i = 0; i < 63; i++) {
    for (long long j = 0; j < 63; j++) {
      dp1[i] += dp[j] * m[i][j][bac];
      dp1[i] %= mod;
    }
  }
  for (long long i = 0; i < 63; i++) {
    dp[i] = dp1[i];
  }
}
void dangyeu1() {
  long long dp1[65];
  memset(dp1, 0, sizeof(dp1));
  for (long long i = 0; i < 63; i++) {
    for (long long j = 0; j < 63; j++) {
      dp1[i] += dp[j] * m[i][j][0];
      dp1[i] %= mod;
    }
  }
  for (long long i = 0; i < 63; i++) {
    dp[i] = dp1[i];
  }
}
void mul(long long dist) {
  for (long long i = 0; i <= 30; i++) {
    long long lol = (1 << i);
    if ((lol & dist) > 0) {
      dangyeu(i);
    }
  }
}
void solve(long long id) {
  long long cur = 3;
  dp[0] = 1;
  for (long long i = 1; i <= min(a[id], 3ll); i++) {
    Dora(lim[id][i], i);
  }
  for (auto i : que[id]) {
    if (i.first <= 3) continue;
    mul(i.first - 1 - cur);
    Dora({i.second, i.second}, i.first);
    cur = i.first;
  }
  if (cur < a[id]) mul(a[id] - cur);
}
signed main() {
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
    for (long long j = 1; j <= 3; j++) {
      lim[i][j] = {1, 3};
    }
  }
  cin >> dora;
  for (long long i = 1; i <= dora; i++) {
    long long t, p, c;
    cin >> t >> p >> c;
    if (p <= 3) {
      lim[t][p] = {c, c};
      continue;
    }
    que[t].push_back({p, c});
  }
  for (long long i = 1; i <= 3; i++) {
    for (long long j = 1; j <= 3; j++) {
      cin >> f[i][j];
    }
  }
  init();
  for (long long ind = 1; ind <= n; ind++) {
    sort(que[ind].begin(), que[ind].end());
    memset(dp, 0, sizeof(dp));
    solve(ind);
    for (long long i = 0; i < 4; i++) {
      for (long long j = 0; j < 4; j++) {
        for (long long k = 0; k < 4; k++) {
          tai_sao_toi_lai_rte_dit_cac[ind][i] += dp[ghash(i, j, k)];
          tai_sao_toi_lai_rte_dit_cac[ind][i] %= mod;
        }
      }
    }
  }
  ans[0][0] = 1;
  for (long long i = 1; i <= n; i++) {
    for (long long j = 0; j < 4; j++) {
      for (long long k = 0; k < 4; k++) {
        ans[i][j ^ k] += tai_sao_toi_lai_rte_dit_cac[i][k] * ans[i - 1][j];
        ans[i][j ^ k] %= mod;
      }
    }
  }
  cout << (ans[n][0]) % mod;
}
