#include <bits/stdc++.h>
using ll = long long;
const ll modp = 998244353;
const int N = 1010;
using namespace std;
struct Mat {
  int n, m;
  ll data[64][64];
  inline ll *operator[](int x) { return data[x]; }
  inline const ll *operator[](int x) const { return data[x]; }
  Mat() : n(64), m(64) {
    for (int i = 0; i < n; i++) std::fill(data[i], data[i] + m, 0);
  }
  Mat(int n, int m, bool is_one) : n(n), m(m) {
    for (int i = 0; i < n; i++) std::fill(data[i], data[i] + m, 0);
    if (is_one) {
      for (int i = 0; i < std::min(n, m); i++) data[i][i] = 1;
    }
  }
  Mat operator*(const Mat &o) const {
    Mat tmp(n, o.m, false);
    for (int i = 0; i < tmp.n; i++)
      for (int j = 0; j < tmp.m; j++) {
        for (int k = 0; k < m; k++)
          tmp[i][j] = (tmp[i][j] + data[i][k] * o[k][j] % modp) % modp;
      }
    return tmp;
  }
  Mat &operator+=(const Mat &o) {
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) data[i][j] += o[i][j];
    return *this;
  }
} full, colored[3], full_powed[32];
inline void PowerFull(Mat &res, ll t) {
  for (int i = 0; i <= 31; i++) {
    if (t & (1ll << i)) {
      res = full_powed[i] * res;
    }
  }
}
int f[3][3], n, a[N], m;
ll g[N][4];
map<int, int> c[N];
int GetSG(vector<int> &pres) {
  int h[4] = {0, 0, 0, 0};
  for (int x : pres) h[x] = 1;
  for (int i = 0; i < 4; i++)
    if (h[i] == 0) return i;
}
void InitTransition() {
  vector<int> pres;
  pres.reserve(3);
  for (int color = 0; color < 3; color++) {
    for (int i = 0; i < 64; i++) {
      pres.clear();
      for (int j = 0; j < 3; j++)
        if (f[color][j]) {
          pres.push_back((i >> (4 - 2 * j)) & 3);
        }
      int ns = (i >> 2) | (GetSG(pres) << 4);
      colored[color][ns][i]++;
    }
    full += colored[color];
  }
  full_powed[0] = full;
  for (int i = 1; i <= 31; i++)
    full_powed[i] = full_powed[i - 1] * full_powed[i - 1];
}
void Calc(int idx, ll g[4]) {
  Mat res(64, 1, false);
  res[63][0] = 1;
  int last = -1;
  for (auto ppc : c[idx]) {
    int p, color;
    std::tie(p, color) = ppc;
    PowerFull(res, p - last - 1);
    res = colored[color] * res;
    last = p;
  }
  PowerFull(res, a[idx] - 1 - last);
  g[0] = g[1] = g[2] = g[3] = 0;
  for (int i = 0; i < 64; i++) g[i >> 4] = (g[i >> 4] + res[i][0]) % modp;
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) scanf("%d", a + i);
  cin >> m;
  for (int i = 0; i < m; i++) {
    int x, y, color;
    scanf("%d%d%d", &x, &y, &color);
    c[x - 1][y - 1] = color - 1;
  }
  for (int i = 0; i < 3; i++)
    for (int j = 0; j < 3; j++) scanf("%d", &f[i][j]);
  InitTransition();
  for (int i = 0; i < n; i++) Calc(i, g[i]);
  ll tmp[4];
  copy(g[0], g[0] + 4, tmp);
  for (int i = 1; i < n; i++) {
    ll newtmp[4] = {0, 0, 0, 0};
    for (int j = 0; j < 4; j++)
      for (int k = 0; k < 4; k++)
        newtmp[j ^ k] = (newtmp[j ^ k] + tmp[j] * g[i][k] % modp) % modp;
    std::copy(newtmp, newtmp + 4, tmp);
  }
  cout << tmp[0] << endl;
  return 0;
}
