#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
const int maxn = 1010, Max = (1 << 6) + 3, mod = 998244353;
int a[maxn], val[maxn][4];
bool f[3][3];
vector<pair<int, int> > v[maxn];
int tmp[Max][Max];
struct matrice {
  int val[Max][Max];
  matrice(bool is = 0) {
    memset(val, 0, sizeof val);
    if (is) {
      for (int i = 0; i < Max; i++) val[i][i] = 1;
    }
  }
  int *operator[](int i) { return val[i]; }
};
matrice t[3], tt[33];
matrice operator*(matrice a, matrice b) {
  matrice ans;
  for (int i = 0; i < Max; i++) {
    for (int j = 0; j < Max; j++) {
      tmp[i][j] = b[j][i];
    }
  }
  for (int i = 0; i < Max; i++) {
    for (int j = 0; j < Max; j++) {
      long long num = 0;
      for (int k = 0; k < Max; k++) {
        num += 1ll * a[i][k] * tmp[j][k];
        if ((k & 7) == 0) num %= mod;
      }
      ans[i][j] = num % mod;
    }
  }
  return ans;
}
void operator*=(array<int, Max> &a, matrice b) {
  memset(tmp, 0, sizeof tmp);
  for (int j = 0; j < Max; j++) {
    for (int i = 0; i < Max; i++) {
      tmp[0][i] = (tmp[0][i] + 1ll * a[j] * b[j][i]) % mod;
    }
  }
  for (int i = 0; i < Max; i++) {
    a[i] = tmp[0][i];
  }
}
void operator^=(array<int, Max> &a, int n) {
  for (int i = 0; n; i++, n >>= 1) {
    if (n & 1) a *= tt[i];
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int m;
  cin >> m;
  while (m--) {
    int a, b, c;
    cin >> a >> b >> c;
    v[--a].push_back({--b, --c});
  }
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> f[i][j];
    }
  }
  for (int i = 0; i < (1 << 6); i++) {
    for (int j = 0; j < 3; j++) {
      int arr[3], cc = 0;
      for (int k = 0; k < 3; k++) {
        if (f[j][k]) arr[cc++] = (((i) >> (2 * (2 - k))) & 3);
      }
      sort(arr, arr + cc);
      cc = unique(arr, arr + cc) - arr;
      int mex = cc;
      for (int k = 0; k < cc; k++) {
        if (arr[k] != k) {
          mex = k;
          break;
        }
      }
      int to = (i >> 2) | (mex << 4);
      t[j][i][to]++;
      tt[0][i][to]++;
    }
  }
  for (int i = 1; i < 33; i++) {
    tt[i] = tt[i - 1] * tt[i - 1];
  }
  for (int i = 0; i < n; i++) {
    sort(v[i].begin(), v[i].end());
    array<int, Max> ans;
    for (int i = 0; i < Max; i++) ans[i] = 0;
    ans[(1 << 6) - 1] = 1;
    int lst = -1;
    for (pair<int, int> p : v[i]) {
      if (p.first == lst) continue;
      ans ^= p.first - lst - 1;
      ans *= t[p.second];
      lst = p.first;
    }
    if (lst != a[i]) ans ^= a[i] - lst - 1;
    for (int j = 0; j < (1 << 6); j++) {
      val[i][(((j) >> (2 * (2))) & 3)] =
          (val[i][(((j) >> (2 * (2))) & 3)] + ans[j]) % mod;
    }
  }
  for (int i = 1; i < n; i++) {
    int tmp[4];
    memset(tmp, 0, sizeof tmp);
    for (int j = 0; j < 4; j++) {
      for (int k = 0; k < 4; k++) {
        tmp[j ^ k] = (1ll * tmp[j ^ k] + 1ll * val[i - 1][j] * val[i][k]) % mod;
      }
    }
    for (int j = 0; j < 4; j++) {
      val[i][j] = tmp[j];
    }
  }
  return cout << val[n - 1][0] << endl, 0;
}
