#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
const int mod = 998244353;
const int INF = 2e9;
int sz[1005];
vector<pair<int, int> > done[1005];
int QQ[1005][4];
int ok[4][4];
long long A[64][64], B[4][64][64];
long long AA[30][64][64];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &sz[i]);
  int m;
  scanf("%d", &m);
  int cnt = n;
  for (int i = 0; i < m; i++) {
    int x, y, c;
    scanf("%d%d%d", &x, &y, &c);
    if (y <= 3)
      QQ[x][y] = c;
    else
      done[x].push_back(make_pair(y, c));
  }
  for (int i = 1; i <= 3; i++)
    for (int j = 1; j <= 3; j++) scanf("%d", &ok[i][j]);
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 4; j++) {
      for (int k = 0; k < 4; k++) {
        int x = i * 16 + j * 4 + k;
        for (int a = 1; a <= 3; a++) {
          bool vis[4] = {0};
          if (ok[a][1]) vis[k] = 1;
          if (ok[a][2]) vis[j] = 1;
          if (ok[a][3]) vis[i] = 1;
          int l = 0;
          while (vis[l] == 1) l++;
          int y = j * 16 + k * 4 + l;
          A[y][x]++;
          B[a][y][x]++;
        }
      }
    }
  }
  for (int i = 0; i < 64; i++)
    for (int j = 0; j < 64; j++) AA[0][i][j] = A[i][j];
  for (int i = 1; i < 30; i++) {
    for (int a = 0; a < 64; a++) {
      for (int b = 0; b < 64; b++) {
        for (int c = 0; c < 64; c++) {
          AA[i][a][c] = (AA[i][a][c] + AA[i - 1][a][b] * AA[i - 1][b][c]) % mod;
        }
      }
    }
  }
  long long ans[2][4] = {0};
  ans[0][0] = 1;
  int ans_cur = 0;
  for (int i = 1; i <= n; i++) {
    if (sz[i] == 1) {
      long long tmp_sum[4] = {0};
      if (QQ[i][1] == 0)
        tmp_sum[0] = 3;
      else
        tmp_sum[0] = 1;
      for (int j = 0; j < 4; j++) ans[ans_cur ^ 1][j] = 0;
      for (int j = 0; j < 4; j++)
        for (int k = 0; k < 4; k++)
          ans[ans_cur ^ 1][j] += ans[ans_cur][k] * tmp_sum[j ^ k];
      for (int j = 0; j < 4; j++) ans[ans_cur ^ 1][j] %= mod;
      ans_cur ^= 1;
      continue;
    } else if (sz[i] == 2) {
      long long tmp_sum[4] = {0};
      for (int a = 1; a <= 3; a++) {
        if (QQ[i][1] != 0 && QQ[i][1] != a) continue;
        for (int b = 1; b <= 3; b++) {
          if (QQ[i][2] != 0 && QQ[i][2] != b) continue;
          int y;
          if (ok[b][1])
            y = 1;
          else
            y = 0;
          tmp_sum[y]++;
        }
      }
      for (int j = 0; j < 4; j++) ans[ans_cur ^ 1][j] = 0;
      for (int j = 0; j < 4; j++)
        for (int k = 0; k < 4; k++)
          ans[ans_cur ^ 1][j] += ans[ans_cur][k] * tmp_sum[j ^ k];
      for (int j = 0; j < 4; j++) ans[ans_cur ^ 1][j] %= mod;
      ans_cur ^= 1;
      continue;
    }
    sort(done[i].begin(), done[i].end());
    int pos = 3;
    int cur = 0;
    long long st[2][64] = {0};
    for (int a = 1; a <= 3; a++) {
      if (QQ[i][1] != 0 && QQ[i][1] != a) continue;
      for (int b = 1; b <= 3; b++) {
        if (QQ[i][2] != 0 && QQ[i][2] != b) continue;
        for (int c = 1; c <= 3; c++) {
          if (QQ[i][3] != 0 && QQ[i][3] != c) continue;
          int y, z;
          if (ok[b][1])
            y = 1;
          else
            y = 0;
          bool vis[4] = {0};
          if (ok[c][1]) vis[y] = 1;
          if (ok[c][2]) vis[0] = 1;
          z = 0;
          while (vis[z] == 1) z++;
          st[0][y * 4 + z]++;
        }
      }
    }
    for (pair<int, int> p : done[i]) {
      if (p.first - 1 - pos > 0) {
        int q = p.first - 1 - pos;
        for (int j = 29; j >= 0; j--) {
          if ((1 << j) <= q) {
            for (int x = 0; x < 64; x++) st[cur ^ 1][x] = 0;
            for (int x = 0; x < 64; x++)
              for (int y = 0; y < 64; y++)
                st[cur ^ 1][x] =
                    (st[cur ^ 1][x] + AA[j][x][y] * st[cur][y]) % mod;
            q -= (1 << j);
            cur ^= 1;
          }
        }
      }
      for (int x = 0; x < 64; x++) st[cur ^ 1][x] = 0;
      for (int x = 0; x < 64; x++)
        for (int y = 0; y < 64; y++)
          st[cur ^ 1][x] =
              (st[cur ^ 1][x] + B[p.second][x][y] * st[cur][y]) % mod;
      cur ^= 1;
      pos = p.first;
    }
    if (pos < sz[i]) {
      int q = sz[i] - pos;
      for (int j = 29; j >= 0; j--) {
        if ((1 << j) <= q) {
          for (int x = 0; x < 64; x++) st[cur ^ 1][x] = 0;
          for (int x = 0; x < 64; x++)
            for (int y = 0; y < 64; y++)
              st[cur ^ 1][x] =
                  (st[cur ^ 1][x] + AA[j][x][y] * st[cur][y]) % mod;
          q -= (1 << j);
          cur ^= 1;
        }
      }
    }
    long long tmp_sum[4] = {0};
    for (int j = 0; j < 64; j++) tmp_sum[j & 3] += st[cur][j];
    for (int j = 0; j < 4; j++) tmp_sum[j] %= mod;
    for (int j = 0; j < 4; j++) ans[ans_cur ^ 1][j] = 0;
    for (int j = 0; j < 4; j++)
      for (int k = 0; k < 4; k++)
        ans[ans_cur ^ 1][j] =
            (ans[ans_cur ^ 1][j] + ans[ans_cur][k] * tmp_sum[j ^ k]) % mod;
    ans_cur ^= 1;
  }
  printf("%lld\n", ans[ans_cur][0]);
  return 0;
}
