#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  long long n;
  cin >> n;
  vector<long long> a((size_t)n);
  vector<map<long long, long long>> b((size_t)n);
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    for (long long j = 0; j < min((long long)4, a[i]); j++) {
      b[i][j] = -1;
    }
    b[i][a[i] - 1] = -1;
  }
  long long m;
  cin >> m;
  for (long long i = 0; i < m; i++) {
    long long x, y, k;
    cin >> x >> y >> k;
    x--, y--, k--;
    b[x][y] = k;
  }
  vector<vector<long long>> f((size_t)3, vector<long long>((size_t)3));
  for (long long i = 0; i < 3; i++) {
    for (long long j = 0; j < 3; j++) {
      cin >> f[i][j];
    }
  }
  auto matrix_vector = [&](vector<vector<long long>> x, vector<long long> y) {
    vector<long long> z((size_t)4 * 4 * 4, 0);
    for (long long i = 0; i < 4 * 4 * 4; i++) {
      for (long long j = 0; j < 4 * 4 * 4; j++) {
        z[i] += x[i][j] * y[j];
        z[i] %= 998244353;
      }
    }
    return z;
  };
  auto matrix_matrix = [&](vector<vector<long long>> x,
                           vector<vector<long long>> y) {
    vector<vector<long long>> z((size_t)4 * 4 * 4,
                                vector<long long>((size_t)4 * 4 * 4, 0));
    for (long long i = 0; i < 4 * 4 * 4; i++) {
      for (long long j = 0; j < 4 * 4 * 4; j++) {
        for (long long k = 0; k < 4 * 4 * 4; k++) {
          z[i][j] += x[i][k] * y[k][j];
          z[i][j] %= 998244353;
        }
      }
    }
    return z;
  };
  vector<vector<long long>> matrix((size_t)4 * 4 * 4,
                                   vector<long long>((size_t)4 * 4 * 4, 0));
  for (long long k = 0; k < 3; k++) {
    for (long long x = 0; x < 4; x++) {
      for (long long y = 0; y < 4; y++) {
        for (long long z = 0; z < 4; z++) {
          long long l = 0;
          while ((f[k][2] == 1 && l == x) || (f[k][1] == 1 && l == y) ||
                 (f[k][0] == 1 && l == z)) {
            l++;
          }
          matrix[y * 4 * 4 + z * 4 + l][x * 4 * 4 + y * 4 + z]++;
        }
      }
    }
  }
  vector<vector<vector<long long>>> matrix_powers((size_t)40);
  matrix_powers[0] = matrix;
  for (long long i = 1; i < 40; i++) {
    matrix_powers[i] =
        matrix_matrix(matrix_powers[i - 1], matrix_powers[i - 1]);
  }
  vector<vector<long long>> c((size_t)n,
                              vector<long long>((size_t)4 * 4 * 4, 0));
  auto do_op = [&](long long i, long long j) {
    vector<long long> d((size_t)4 * 4 * 4, 0);
    for (long long k = b[i].find(j) == b[i].end() || b[i][j] == -1 ? 0
                                                                   : b[i][j];
         b[i].find(j) == b[i].end() || b[i][j] == -1 ? k < 3 : k <= b[i][j];
         k++) {
      for (long long x = 0; x < 4; x++) {
        for (long long y = 0; y < 4; y++) {
          for (long long z = 0; z < 4; z++) {
            long long l = 0;
            while ((j >= 3 && f[k][2] == 1 && l == x) ||
                   (j >= 2 && f[k][1] == 1 && l == y) ||
                   (j >= 1 && f[k][0] == 1 && l == z)) {
              l++;
            }
            d[y * 4 * 4 + z * 4 + l] += c[i][x * 4 * 4 + y * 4 + z];
            d[y * 4 * 4 + z * 4 + l] %= 998244353;
          }
        }
      }
    }
    c[i] = d;
  };
  for (long long i = 0; i < n; i++) {
    c[i][0 * 4 * 4 + 0 * 4 + 0] = 1;
    long long j = -1;
    for (pair<long long, long long> p : b[i]) {
      if (j + 1 <= p.first - 1) {
        vector<long long> v((size_t)4 * 4);
        for (long long k = 0; k < 31; k++) {
          if (((p.first - j - 1) & (1 << k)) != 0) {
            c[i] = matrix_vector(matrix_powers[k], c[i]);
          }
        }
      }
      do_op(i, p.first);
      j = p.first;
    }
  }
  vector<long long> e((size_t)4, 0);
  e[0] = 1;
  for (long long i = 0; i < n; i++) {
    vector<long long> g((size_t)4, 0);
    for (long long j = 0; j < 4; j++) {
      for (long long x = 0; x < 4; x++) {
        for (long long y = 0; y < 4; y++) {
          for (long long z = 0; z < 4; z++) {
            g[j ^ z] += e[j] * c[i][x * 4 * 4 + y * 4 + z];
            g[j ^ z] %= 998244353;
          }
        }
      }
    }
    e = g;
  }
  cout << e[0] << endl;
  return 0;
}
