#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
const int MOD = 998244353;
const int MAXN = 1000;
const int MAXCOL = 3;
const int MAXSTEP = 3;
const int MAXGRUNDY = 3;
const int MAXSTATE = (MAXGRUNDY + 1) * (MAXGRUNDY + 1) * (MAXGRUNDY + 1);
const int MAXLG = 30;
const long long MODUNDOOVERFLOW = (1ULL << 63) / MOD * MOD;
void inc(int &a, int b) {
  if ((a += b) >= MOD) a -= MOD;
}
int n;
int sz[MAXN];
int nknown;
vector<pair<int, int>> known[MAXN];
int allowed[MAXCOL][MAXSTEP + 1];
vector<vector<int>> mat[MAXCOL + 1];
int stripways[MAXN][MAXGRUNDY + 1];
int dp[MAXN + 1][MAXGRUNDY + 1];
vector<vector<int>> matpw[MAXCOL + 1][MAXLG + 1];
int mex(int mask) {
  int ret = 0;
  while (mask & (1 << ret)) ++ret;
  return ret;
}
int encode(int a, int b, int c) {
  assert(0 <= a && a <= MAXGRUNDY && 0 <= b && b <= MAXGRUNDY && 0 <= c &&
         c <= MAXGRUNDY);
  return a * (MAXGRUNDY + 1) * (MAXGRUNDY + 1) + b * (MAXGRUNDY + 1) + c;
}
void decode(int code, int &a, int &b, int &c) {
  c = code % 4;
  code /= 4;
  b = code % 4;
  code /= 4;
  a = code;
}
bool reachable[MAXSTATE];
int mp[MAXSTATE], invmp[MAXSTATE];
vector<vector<int>> mmlt(vector<vector<int>> A, vector<vector<int>> B) {
  assert(((int)(A).size()) >= 1 && ((int)(B).size()) >= 1 &&
         ((int)(A[1]).size()) == ((int)(B).size()));
  int n = ((int)(A).size()), k = ((int)(A[0]).size()), m = ((int)(B[0]).size());
  vector<vector<int>> ret(n, vector<int>(m, 0));
  for (int i = (0); i < (n); ++i)
    for (int j = (0); j < (n); ++j)
      for (int x = (0); x < (k); ++x) {
        long long sum = 0;
        for (int x = (0); x < (n); ++x) {
          sum += (long long)A[i][x] * B[x][j];
          if (sum < 0) sum -= MODUNDOOVERFLOW;
        }
        sum %= MOD;
        ret[i][j] = sum;
      }
  return ret;
}
vector<vector<int>> mpw(vector<vector<int>> x, int n) {
  int dim = ((int)(x).size());
  assert(dim >= 1 && ((int)(x[0]).size()) == dim);
  vector<vector<int>> ret(dim, vector<int>(dim, 0));
  for (int i = (0); i < (dim); ++i) ret[i][i] = 1;
  while (true) {
    if (n & 1) ret = mmlt(ret, x);
    if ((n >>= 1) == 0) return ret;
    x = mmlt(x, x);
  }
}
vector<int> combine(vector<int> ways, vector<vector<int>> mat) {
  int dim = ((int)(ways).size());
  assert(dim >= 1 && ((int)(mat).size()) == dim &&
         ((int)(mat[0]).size()) == dim);
  vector<int> ret(dim, 0);
  for (int i = (0); i < (dim); ++i)
    for (int j = (0); j < (dim); ++j)
      ret[j] = (ret[j] + (long long)ways[i] * mat[i][j]) % MOD;
  return ret;
}
int solve() {
  memset(reachable, false, sizeof(reachable));
  queue<int> q;
  for (int g0 = (0); g0 <= (MAXGRUNDY); ++g0) {
    if (g0 != 0) continue;
    for (int g1 = (0); g1 <= (MAXGRUNDY); ++g1) {
      if (g1 != 0 && g1 != mex(1 << g0)) continue;
      for (int g2 = (0); g2 <= (MAXGRUNDY); ++g2) {
        if (g2 != 0 && g2 != mex(1 << g0) && g2 != mex(1 << g1) &&
            g2 != mex((1 << g0) | (1 << g1)))
          continue;
        int code = encode(g0, g1, g2);
        reachable[code] = true;
        q.push(code);
      }
    }
  }
  while (!q.empty()) {
    int at = q.front();
    q.pop();
    for (int col = (0); col < (MAXCOL); ++col) {
      int g0, g1, g2;
      decode(at, g0, g1, g2);
      int mask = 0;
      if (allowed[col][1]) mask |= 1 << g2;
      if (allowed[col][2]) mask |= 1 << g1;
      if (allowed[col][3]) mask |= 1 << g0;
      int g3 = mex(mask);
      int to = encode(g1, g2, g3);
      if (!reachable[to]) reachable[to] = true, q.push(to);
    }
  }
  int nreachable = 0;
  for (int i = (0); i < (MAXSTATE); ++i)
    if (reachable[i])
      mp[i] = nreachable, invmp[nreachable] = i, ++nreachable;
    else
      mp[i] = -1;
  for (int i = (0); i <= (MAXCOL); ++i) {
    mat[i] = vector<vector<int>>(nreachable, vector<int>(nreachable, 0));
    for (int g0 = (0); g0 <= (MAXGRUNDY); ++g0)
      for (int g1 = (0); g1 <= (MAXGRUNDY); ++g1)
        for (int g2 = (0); g2 <= (MAXGRUNDY); ++g2)
          for (int col = (0); col < (MAXCOL); ++col) {
            if (i < MAXCOL && col != i) continue;
            int mask = 0;
            if (allowed[col][1]) mask |= 1 << g2;
            if (allowed[col][2]) mask |= 1 << g1;
            if (allowed[col][3]) mask |= 1 << g0;
            int g3 = mex(mask);
            int a = mp[encode(g0, g1, g2)], b = mp[encode(g1, g2, g3)];
            if (a != -1 && b != -1) inc(mat[i][a][b], 1);
          }
  }
  for (int i = (0); i <= (MAXCOL); ++i) {
    matpw[i][0] = mat[i];
    for (int k = (1); k <= (MAXLG); ++k)
      matpw[i][k] = mmlt(matpw[i][k - 1], matpw[i][k - 1]);
  }
  for (int i = (0); i < (n); ++i) {
    for (int j = (0); j <= (MAXGRUNDY); ++j) stripways[i][j] = 0;
    sort(known[i].begin(), known[i].end());
    vector<int> col(MAXSTEP, -1);
    for (int j = (0); j < (((int)(known[i]).size())); ++j)
      if (known[i][j].first < ((int)(col).size()))
        col[known[i][j].first] = known[i][j].second;
    for (int j = (sz[i]); j < (((int)(col).size())); ++j) col[j] = 0;
    vector<int> ways(nreachable, 0);
    for (int a = (0); a < (MAXCOL); ++a)
      for (int b = (0); b < (MAXCOL); ++b)
        for (int c = (0); c < (MAXCOL); ++c) {
          if (col[0] != -1 && col[0] != a) continue;
          if (col[1] != -1 && col[1] != b) continue;
          if (col[2] != -1 && col[2] != c) continue;
          vector<int> g(MAXSTEP);
          int amask = 0;
          g[0] = mex(amask);
          int bmask = 0;
          if (allowed[b][1]) bmask |= 1 << g[0];
          g[1] = mex(bmask);
          int cmask = 0;
          if (allowed[c][1]) cmask |= 1 << g[1];
          if (allowed[c][2]) cmask |= 1 << g[0];
          g[2] = mex(cmask);
          int code = encode(g[0], g[1], g[2]);
          if (sz[i] == 1)
            inc(stripways[i][g[0]], 1);
          else if (sz[i] == 2)
            inc(stripways[i][g[1]], 1);
          else if (sz[i] == 3)
            inc(stripways[i][g[2]], 1);
          else {
            assert(mp[code] != -1);
            inc(ways[mp[code]], 1);
          }
        }
    if (sz[i] > 3) {
      int last = 2;
      vector<pair<int, int>> e;
      for (int j = (0); j < (((int)(known[i]).size())); ++j)
        if (known[i][j].first >= ((int)(col).size())) e.push_back(known[i][j]);
      e.push_back(make_pair(sz[i], 0));
      for (int j = (0); j < (((int)(e).size())); ++j) {
        int nfree = e[j].first - last - 1;
        for (int k = (0); k <= (MAXLG); ++k)
          if (nfree & (1 << k)) ways = combine(ways, matpw[MAXCOL][k]);
        ways = combine(ways, mat[e[j].second]);
        last = e[j].first;
      }
      for (int mappedcode = (0); mappedcode < (((int)(ways).size()));
           ++mappedcode) {
        int code = invmp[mappedcode], g0, g1, g2;
        decode(code, g0, g1, g2);
        inc(stripways[i][g1], ways[mappedcode]);
      }
    }
  }
  for (int j = (0); j <= (MAXGRUNDY); ++j) dp[0][j] = 0;
  inc(dp[0][0], 1);
  for (int i = (0); i < (n); ++i) {
    for (int j = (0); j <= (MAXGRUNDY); ++j) dp[i + 1][j] = 0;
    for (int j = (0); j <= (MAXGRUNDY); ++j)
      for (int k = (0); k <= (MAXGRUNDY); ++k)
        dp[i + 1][j ^ k] =
            (dp[i + 1][j ^ k] + (long long)dp[i][j] * stripways[i][k]) % MOD;
  }
  return dp[n][0];
}
void run() {
  scanf("%d", &n);
  for (int i = (0); i < (n); ++i) scanf("%d", &sz[i]);
  scanf("%d", &nknown);
  for (int i = (0); i < (nknown); ++i) {
    int x, y, z;
    scanf("%d%d%d", &x, &y, &z);
    --x, --y, --z;
    known[x].push_back(make_pair(y, z));
  }
  for (int i = (0); i < (MAXCOL); ++i)
    for (int j = (1); j <= (MAXSTEP); ++j) scanf("%d", &allowed[i][j]);
  printf("%d\n", solve());
}
int main() {
  run();
  return 0;
}
