#include <bits/stdc++.h>
using namespace std;
const int64_t MOD = 998244353;
void add(int64_t& a, int64_t b) { a = (a + b) % MOD; }
void mul(int64_t& a, int64_t b) { a = a * b % MOD; }
const int SZ = 4 * 4 * 4;
void matmul(int64_t A[SZ][SZ], int64_t B[SZ][SZ]) {
  int64_t ret[SZ][SZ] = {0};
  for (int i = 0; i < SZ; i++)
    for (int j = 0; j < SZ; j++)
      for (int k = 0; k < SZ; k++) add(ret[i][j], A[i][k] * B[k][j]);
  for (int i = 0; i < SZ; i++)
    for (int j = 0; j < SZ; j++) A[i][j] = ret[i][j];
}
void mulV(int64_t A[SZ], int64_t B[SZ][SZ]) {
  int64_t ret[SZ] = {0};
  for (int j = 0; j < SZ; j++)
    for (int k = 0; k < SZ; k++) add(ret[k], A[j] * B[k][j]);
  for (int i = 0; i < SZ; i++) A[i] = ret[i];
}
int encode(vector<int>& G) {
  int ret = 0;
  int base = 1;
  for (int g : G) {
    ret += base * g;
    base *= 4;
  }
  return ret;
}
vector<int> decode(int X) {
  vector<int> G;
  for (int i = 0; i < 3; i++) {
    G.push_back(X % 4);
    X /= 4;
  }
  return G;
}
static int64_t X[30][SZ][SZ] = {};
void mulX(int64_t A[SZ], int K) {
  int lev = 0;
  while (K > 0) {
    if (K % 2) mulV(A, X[lev]);
    lev++;
    K /= 2;
  }
}
int main() {
  int N, A[1000];
  cin >> N;
  for (int i = 0; i < N; i++) cin >> A[i];
  int M;
  cin >> M;
  vector<pair<int, int>> fix[1000];
  for (int i = 0; i < M; i++) {
    int x, y, c;
    cin >> x >> y >> c;
    x--;
    y--;
    c--;
    fix[x].emplace_back(y, c);
  }
  for (int i = 0; i < N; i++) {
    fix[i].push_back({-1, -1});
    sort(fix[i].begin(), fix[i].end());
    if (fix[i].back().first != A[i] - 1) fix[i].emplace_back(A[i] - 1, -1);
  }
  int F[3][3];
  for (int i = 0; i < 3; i++)
    for (int j = 0; j < 3; j++) cin >> F[i][j];
  int64_t col1[3][SZ][SZ] = {0};
  for (int code = 0; code < SZ; code++) {
    auto G = decode(code);
    for (int c = 0; c < 3; c++) {
      bitset<4> gr;
      for (int i = 0; i < 3; i++)
        if (F[c][i]) gr[G[i]] = 1;
      int g = 0;
      while (gr[g]) g++;
      vector<int> G2 = {g, G[0], G[1]};
      X[0][encode(G2)][code]++;
      col1[c][encode(G2)][code]++;
    }
  }
  for (int i = 1; i < 30; i++) {
    for (int k = 0; k < SZ; k++) X[i][k][k] = 1;
    for (int k = 0; k < 2; k++) matmul(X[i], X[i - 1]);
  }
  int64_t R[1000][4] = {0};
  for (int i = 0; i < N; i++) {
    int64_t Y[SZ] = {0};
    vector<int> G = {3, 3, 3};
    Y[encode(G)] = 1;
    auto& f = fix[i];
    int sz = f.size();
    for (int j = 1; j < sz; j++) {
      int d = f[j].first - f[j - 1].first;
      int c = f[j].second;
      if (c == -1) {
        mulX(Y, d);
      } else {
        mulX(Y, d - 1);
        mulV(Y, col1[c]);
      }
    }
    for (int code = 0; code < SZ; code++) {
      auto G = decode(code);
      add(R[i][G[0]], Y[code]);
    }
  }
  int64_t dp[1001][4] = {0};
  dp[0][0] = 1;
  for (int i = 0; i < N; i++)
    for (int j = 0; j < 4; j++)
      for (int k = 0; k < 4; k++) add(dp[i + 1][j ^ k], R[i][k] * dp[i][j]);
  cout << dp[N][0] << endl;
  return 0;
}
