#include <bits/stdc++.h>
using namespace std;
const long long Mod = 998244353;
const int maxn = 1010;
const int mn = 64;
void upd(long long &first, long long v) { first = (first + v) % Mod; }
struct mat {
  int n, m;
  long long a[mn][mn];
  void clear() {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        a[i][j] = 0;
      }
    }
  }
  void init() {
    clear();
    for (int i = 0; i < mn; i++) {
      a[i][i] = 1;
    }
  }
  mat operator*(const mat &b) const {
    mat c;
    c.n = n;
    c.m = b.m;
    c.clear();
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < b.m; j++) {
        for (int k = 0; k < m; k++) {
          upd(c.a[i][j], a[i][k] * b.a[k][j]);
        }
      }
    }
    return c;
  }
} p[35], c[4];
int n, m, a[maxn], tmp[4];
long long f[maxn][4], g[maxn][4];
bool can[4][4], used[4];
vector<pair<int, int> > v[maxn];
int ti;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    v[i].push_back(make_pair(a[i] + 1, 0));
  }
  scanf("%d", &m);
  for (int i = 0; i < m; i++) {
    int first, second, z;
    scanf("%d%d%d", &first, &second, &z);
    v[first].push_back(make_pair(second, z));
  }
  for (int i = 1; i <= n; i++) {
    sort(v[i].begin(), v[i].end());
  }
  for (int i = 1; i <= 3; i++) {
    for (int j = 1; j <= 3; j++) {
      int first;
      scanf("%d", &first);
      if (first) {
        can[i][j] = true;
      }
    }
  }
  for (int i = 0; i < 30; i++) {
    p[i].n = p[i].m = mn;
  }
  for (int i = 0; i < 4; i++) {
    c[i].n = c[i].m = mn;
  }
  for (int mask = 0; mask < mn; mask++) {
    for (int i = 0; i < 3; i++) {
      tmp[i + 1] = (mask >> (i << 1)) & 3;
    }
    for (int i = 1; i <= 3; i++) {
      memset(used, 0, sizeof(used));
      for (int j = 1; j <= 3; j++) {
        if (can[i][j]) {
          used[tmp[j]] = true;
        }
      }
      int sg;
      for (int j = 0; j < 4; j++) {
        if (!used[j]) {
          sg = j;
          break;
        }
      }
      int nxt = sg + (tmp[1] << 2) + (tmp[2] << 4);
      upd(p[0].a[nxt][mask], 1);
      upd(c[i].a[nxt][mask], 1);
    }
  }
  for (int i = 1; i < 30; i++) {
    p[i] = p[i - 1] * p[i - 1];
  }
  for (int i = 1; i <= n; i++) {
    int lst = 0;
    mat now;
    now.n = mn;
    now.m = 1;
    now.clear();
    now.a[63][0] = 1;
    for (int j = 0; j < v[i].size(); j++) {
      int second = v[i][j].first - lst - 1;
      for (int k = 0; k < 30; k++) {
        if (second & (1 << k)) {
          now = p[k] * now;
        }
      }
      if (v[i][j].first != a[i] + 1) {
        now = c[v[i][j].second] * now;
      }
      lst = v[i][j].first;
    }
    for (int j = 0; j < mn; j++) {
      upd(g[i][j & 3], now.a[j][0]);
    }
  }
  f[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < 4; j++) {
      for (int k = 0; k < 4; k++) {
        upd(f[i][j ^ k], f[i - 1][j] * g[i][k]);
      }
    }
  }
  cout << f[n][0] << endl;
  return 0;
}
