#include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;
using VP = vector<P>;
using ll = long long;
using VL = vector<ll>;
using VVL = vector<VL>;
using VVVL = vector<VVL>;
using VI = vector<int>;
using VVI = vector<VI>;
using SI = set<int>;
const ll MOD = 998244353;
VVVL PW(31, VVL(64, VL(64, 0)));
inline int ajunta(int a, int b, int c) { return (a << 4) + (b << 2) + c; }
inline int mira(int masc, int z) { return (masc >> (z << 1)) & 3; }
inline void suma(ll& x, ll y) { x = (x + y) % MOD; }
ll prod(const VL& A, const VL& B) {
  ll res = 0;
  for (int j = 0; j < 64; ++j) suma(res, A[j] * B[j]);
  return res;
}
VL prod(const VVL& mat, const VL& V) {
  VL res(64);
  for (int j = 0; j < 64; ++j) res[j] = prod(mat[j], V);
  return res;
}
VVL prod(const VVL& mat1, const VVL& mat2) {
  VVL res(64, VL(64, 0));
  for (int i = 0; i < 64; ++i)
    for (int j = 0; j < 64; ++j)
      for (int k = 0; k < 64; ++k) suma(res[i][j], mat1[i][k] * mat2[k][j]);
  return res;
}
void multiplica(int pw, VL& V) {
  for (int i = 0; i <= 30; ++i)
    if ((pw >> i) & 1) V = prod(PW[i], V);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  VI A(n);
  for (int& a : A) cin >> a;
  vector<VP> col(n);
  int m;
  cin >> m;
  while (m--) {
    int x, y, c;
    cin >> x >> y >> c;
    col[x - 1].push_back(P(y - 1, c - 1));
  }
  for (int k = 0; k < n; ++k) sort(col[k].begin(), col[k].end());
  VVI F(3, VI(3));
  for (int i = 0; i < 3; ++i)
    for (int j = 0; j < 3; ++j) cin >> F[i][j];
  VVVL M(3, VVL(64, VL(64, 0)));
  for (int i = 0; i < 3; ++i)
    for (int x = 0; x < 64; ++x) {
      SI S = {0, 1, 2, 3};
      for (int j = 0; j < 3; ++j)
        if (F[i][j]) S.erase(mira(x, j));
      M[i][ajunta(mira(x, 1), mira(x, 0), *S.begin())][x] = 1;
    }
  for (int i = 0; i < 3; ++i)
    for (int x = 0; x < 64; ++x)
      for (int y = 0; y < 64; ++y) PW[0][x][y] += M[i][x][y];
  for (int i = 1; i <= 30; ++i) PW[i] = prod(PW[i - 1], PW[i - 1]);
  VVL R(n, VL(4, 0));
  for (int k = 0; k < n; ++k) {
    VL Q(64, 0);
    Q[ajunta(3, 3, 3)] = 1;
    int pos = -1;
    for (int p = 0; p < (int)col[k].size(); ++p) {
      multiplica(col[k][p].first - pos - 1, Q);
      Q = prod(M[col[k][p].second], Q);
      pos = col[k][p].first;
    }
    multiplica(A[k] - pos - 1, Q);
    for (int x = 0; x < 64; ++x) suma(R[k][mira(x, 0)], Q[x]);
  }
  VL res = R[0];
  for (int k = 1; k < n; ++k) {
    VL V(4, 0);
    for (int i = 0; i < 4; ++i)
      for (int j = 0; j < 4; ++j) suma(V[i ^ j], res[i] * R[k][j]);
    swap(res, V);
  }
  cout << res[0] << endl;
}
