#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr int MOD = 998244353;
class mint {
  int n;

 public:
  mint(int n_ = 0) : n(n_) {}
  explicit operator int() { return n; }
  friend mint operator-(mint a) { return -a.n + MOD * (a.n != 0); }
  friend mint operator+(mint a, mint b) {
    int x = a.n + b.n;
    return x - (x >= MOD) * MOD;
  }
  friend mint operator-(mint a, mint b) {
    int x = a.n - b.n;
    return x + (x < 0) * MOD;
  }
  friend mint operator*(mint a, mint b) { return (long long)a.n * b.n % MOD; }
  friend mint &operator+=(mint &a, mint b) { return a = a + b; }
  friend mint &operator-=(mint &a, mint b) { return a = a - b; }
  friend mint &operator*=(mint &a, mint b) { return a = a * b; }
  friend bool operator==(mint a, mint b) { return a.n == b.n; }
  friend bool operator!=(mint a, mint b) { return a.n != b.n; }
  friend istream &operator>>(istream &i, mint &a) { return i >> a.n; }
  friend ostream &operator<<(ostream &o, mint a) { return o << a.n; }
};
template <class T>
struct matrix {
  int n;
  vector<T> dat;
  matrix(int n_) : n(n_), dat(n_ * n_) {}
  matrix(initializer_list<initializer_list<T>> a) {
    n = a.size();
    dat.reserve(n * n);
    for (initializer_list<T> x : a) {
      assert(x.size() == n);
      for (T y : x) {
        dat.push_back(y);
      }
    }
  }
  T &operator()(int i, int j) {
    assert(0 <= i && i < n && 0 <= j && j < n);
    return dat[i * n + j];
  }
};
template <class T>
matrix<T> operator*(matrix<T> a, matrix<T> b) {
  const int n = a.n;
  matrix<T> c(n);
  for (int i = 0; i < n; i++) {
    for (int k = 0; k < n; k++) {
      for (int j = 0; j < n; j++) {
        c(i, j) += a(i, k) * b(k, j);
      }
    }
  }
  return c;
}
template <class T>
matrix<T> &operator*=(matrix<T> &a, matrix<T> b) {
  return a = a * b;
}
vector<matrix<mint>> tmp0;
vector<matrix<mint>> tmp1;
vector<matrix<mint>> tmp2;
template <class T>
matrix<T> pow(matrix<T> a, long long b) {
  matrix<T> res(a.n);
  for (int i = 0; i < a.n; i++) {
    res(i, i) = 1;
  }
  for (; b > 0; a *= a, b >>= 1) {
    if (b & 1) res *= a;
  }
  return res;
}
template <class T>
ostream &operator<<(ostream &o, matrix<T> a) {
  o << '[';
  for (int i = 0; i < a.n; i++) {
    if (i > 0) o << ' ';
    o << '[';
    for (int j = 0; j < a.n; j++) {
      if (j > 0) o << ' ';
      o << a(i, j);
    }
    o << ']';
  }
  o << ']';
  return o;
}
vector<mint> prod(matrix<mint> a, vector<mint> b) {
  const int n = b.size();
  vector<mint> ans(n);
  for (int i = 0; i < (n); i++)
    for (int j = 0; j < (n); j++) {
      ans[i] += a(i, j) * b[j];
    }
  return ans;
}
vector<mint> fastprod(long long b, vector<mint> x) {
  x = prod(tmp0[b & 0x3ff], x);
  x = prod(tmp1[b >> 10 & 0x3ff], x);
  x = prod(tmp2[b >> 20 & 0x3ff], x);
  return x;
}
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < (N); i++) cin >> A[i];
  int M;
  cin >> M;
  vector<int> X(M), Y(M), C(M);
  vector<vector<pair<int, int>>> events(N);
  for (int i = 0; i < (M); i++) {
    int x, y, c;
    cin >> x >> y >> c;
    events[x - 1].emplace_back(y - 1, c - 1);
  }
  for (int i = 0; i < (N); i++) sort(events[i].begin(), events[i].end());
  vector<vector<int>> F(3, vector<int>(3));
  for (int i = 0; i < (3); i++)
    for (int j = 0; j < (3); j++) cin >> F[i][j];
  map<array<int, 3>, int> mp;
  queue<array<int, 3>> q;
  vector<array<int, 3>> inv;
  q.push({3, 3, 3});
  mp[{3, 3, 3}] = 0;
  vector<vector<int>> G;
  vector<vector<int>> GC[3];
  G.emplace_back();
  for (int i = 0; i < (3); i++) GC[i].emplace_back();
  inv.push_back({3, 3, 3});
  while (!q.empty()) {
    auto a = q.front();
    q.pop();
    for (int i = 0; i < 3; i++) {
      int x = 0;
      for (int j = 0; j < 3; j++) {
        if (F[i][j]) {
          x |= 1 << a[j];
        }
      }
      int y = 0;
      while (x >> y & 1) y++;
      auto b = a;
      b[2] = b[1];
      b[1] = b[0];
      b[0] = y;
      if (!mp.count(b)) {
        mp[b] = G.size();
        G.emplace_back();
        for (int j = 0; j < (3); j++) GC[j].emplace_back();
        inv.push_back(b);
        q.push(b);
      }
      G[mp[a]].push_back(mp[b]);
      GC[i][mp[a]].push_back(mp[b]);
    }
  }
  const int Z = G.size();
  matrix<mint> D(Z);
  vector<matrix<mint>> DC(3, matrix<mint>(Z));
  for (int i = 0; i < (Z); i++)
    for (int j : G[i]) {
      D(j, i) += 1;
    }
  for (int k = 0; k < (3); k++)
    for (int i = 0; i < (Z); i++)
      for (int j : GC[k][i]) {
        DC[k](j, i) += 1;
      }
  matrix<mint> E(Z);
  for (int i = 0; i < (Z); i++) E(i, i) = 1;
  tmp0.push_back(E);
  tmp1.push_back(E);
  tmp2.push_back(E);
  auto D2 = pow(D, 1 << 10);
  auto D3 = pow(D, 1 << 20);
  for (int i = 0; i < (1 << 10); i++) {
    tmp0.push_back(tmp0.back() * D);
    tmp1.push_back(tmp1.back() * D2);
    tmp2.push_back(tmp2.back() * D3);
  }
  vector<mint> dp0(4);
  dp0[0] = 1;
  for (int i = 0; i < (N); i++) {
    vector<mint> B(Z);
    B[0] = 1;
    int k = -1;
    for (auto e : events[i]) {
      B = fastprod(e.first - k - 1, B);
      B = prod(DC[e.second], B);
      k = e.first;
    }
    B = fastprod(A[i] - k - 1, B);
    vector<mint> dp1(4);
    for (int j = 0; j < (4); j++) {
      for (int k = 0; k < (Z); k++) {
        dp1[j ^ inv[k][0]] += dp0[j] * B[k];
      }
    }
    dp0 = dp1;
  }
  cout << dp0[0] << endl;
}
