#include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
const int LOG = 32;
const int MATRIX_SIZE = 64;
constexpr int createKey(int g1, int g2, int g3) {
  return g1 * 16 + g2 * 4 + g3;
}
vector<vector<int>> sqr(const vector<vector<int>> &a) {
  vector<vector<int>> result(MATRIX_SIZE, vector<int>(MATRIX_SIZE, 0));
  for (int i = 0; i < MATRIX_SIZE; i++) {
    for (int j = 0; j < MATRIX_SIZE; j++) {
      for (int k = 0; k < MATRIX_SIZE; k++) {
        result[i][j] = (result[i][j] + 1LL * a[i][k] * a[k][j]) % MOD;
      }
    }
  }
  return result;
}
vector<int> mult(const vector<int> &v, const vector<vector<int>> &m) {
  vector<int> result(MATRIX_SIZE, 0);
  for (int j = 0; j < MATRIX_SIZE; j++) {
    for (int k = 0; k < MATRIX_SIZE; k++) {
      result[j] = (result[j] + 1LL * v[k] * m[k][j]) % MOD;
    }
  }
  return result;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int n;
  cin >> n;
  vector<int> sizes(n);
  for (int i = 0; i < n; i++) {
    cin >> sizes[i];
  }
  int m;
  cin >> m;
  vector<vector<pair<int, int>>> colored(n);
  for (int i = 0; i < m; i++) {
    int x, y, c;
    cin >> x >> y >> c;
    colored[x - 1].emplace_back(y - 1, c - 1);
  }
  vector<vector<int>> f(3, vector<int>(3));
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> f[i][j];
    }
  }
  vector<vector<vector<int>>> oneColor(
      3, vector<vector<int>>(MATRIX_SIZE, vector<int>(MATRIX_SIZE, 0)));
  vector<vector<vector<int>>> powsT(LOG);
  {
    vector<int> g(3);
    for (g[0] = 0; g[0] <= 3; g[0]++) {
      for (g[1] = 0; g[1] <= 3; g[1]++) {
        for (g[2] = 0; g[2] <= 3; g[2]++) {
          for (int color = 0; color < 3; color++) {
            int mex;
            for (int i = 0;; i++) {
              bool ok = true;
              for (int move = 0; move < 3; move++) {
                if (f[color][move]) {
                  ok &= g[2 - move] != i;
                }
              }
              if (ok) {
                mex = i;
                break;
              }
            }
            oneColor[color][createKey(g[0], g[1], g[2])]
                    [createKey(g[1], g[2], mex)]++;
          }
        }
      }
    }
    powsT[0].assign(MATRIX_SIZE, vector<int>(MATRIX_SIZE, 0));
    for (int i = 0; i < MATRIX_SIZE; i++) {
      for (int j = 0; j < MATRIX_SIZE; j++) {
        for (int color = 0; color < 3; color++) {
          powsT[0][i][j] += oneColor[color][i][j];
        }
      }
    }
  }
  for (int i = 1; i < LOG; i++) {
    powsT[i] = sqr(powsT[i - 1]);
  }
  auto skip = [&](vector<int> &cur, int length) {
    if (length <= 0) {
      return;
    }
    for (int i = 0; i < LOG; i++) {
      if (length & (1 << i)) {
        cur = mult(cur, powsT[i]);
      }
    }
  };
  auto processLine = [&](int length, const vector<pair<int, int>> &colored) {
    vector<int> cur(MATRIX_SIZE, 0);
    cur[createKey(3, 3, 3)] = 1;
    int cell = 0;
    for (const auto &p : colored) {
      skip(cur, p.first - cell);
      cur = mult(cur, oneColor[p.second]);
      cell = p.first + 1;
    }
    skip(cur, length - cell);
    vector<int> result(4, 0);
    for (int g0 = 0; g0 <= 3; g0++) {
      for (int g1 = 0; g1 <= 3; g1++) {
        for (int g2 = 0; g2 <= 3; g2++) {
          result[g2] = (1LL * result[g2] + cur[createKey(g0, g1, g2)]) % MOD;
        }
      }
    }
    return result;
  };
  vector<int> dp(4, 0);
  dp[0] = 1;
  for (int line = 0; line < n; line++) {
    sort(colored[line].begin(), colored[line].end());
    auto cur = processLine(sizes[line], colored[line]);
    vector<int> newDp(4, 0);
    for (int i = 0; i <= 3; i++) {
      for (int j = 0; j <= 3; j++) {
        int g = i ^ j;
        newDp[g] = (newDp[g] + 1LL * dp[i] * cur[j]) % MOD;
      }
    }
    swap(dp, newDp);
  }
  cout << dp[0] << '\n';
}
