#include <bits/stdc++.h>
using namespace std;
const int N = 100 * 1000;
const long long mod = 777777777;
long long dp[4 * N + 10][3][3];
int good[3][3];
void build(int pos, int l, int r) {
  if (l == r) {
    for (int i = 0; i < 3; ++i)
      for (int j = 0; j < 3; ++j) dp[pos][i][j] = 0;
    for (int i = 0; i < 3; ++i) dp[pos][i][i] = 1;
    return;
  }
  int childLeft = 2 * pos + 1, childRight = 2 * pos + 2;
  int m = (l + r) / 2;
  build(childLeft, l, m);
  build(childRight, m + 1, r);
  for (int i = 0; i < 3; ++i)
    for (int j = 0; j < 3; ++j) dp[pos][i][j] = 0;
  for (int i = 0; i < 3; ++i)
    for (int j = 0; j < 3; ++j)
      for (int bodl = 0; bodl < 3; ++bodl)
        for (int bodr = 0; bodr < 3; ++bodr)
          if (good[bodl][bodr]) {
            dp[pos][i][j] += dp[childLeft][i][bodl] * dp[childRight][bodr][j];
            dp[pos][i][j] %= mod;
          }
}
void update(int pos, int l, int r, int where, int newVal) {
  if (l == where && r == where) {
    for (int i = 0; i < 3; ++i)
      for (int j = 0; j < 3; ++j) dp[pos][i][j] = 0;
    for (int i = 0; i < 3; ++i)
      if (i == newVal || newVal == -1) dp[pos][i][i] = 1;
    return;
  }
  int childLeft = 2 * pos + 1, childRight = 2 * pos + 2;
  int m = (l + r) / 2;
  if (where <= m)
    update(childLeft, l, m, where, newVal);
  else
    update(childRight, m + 1, r, where, newVal);
  for (int i = 0; i < 3; ++i)
    for (int j = 0; j < 3; ++j) dp[pos][i][j] = 0;
  for (int i = 0; i < 3; ++i)
    for (int j = 0; j < 3; ++j)
      for (int bodl = 0; bodl < 3; ++bodl)
        for (int bodr = 0; bodr < 3; ++bodr)
          if (good[bodl][bodr]) {
            dp[pos][i][j] += dp[childLeft][i][bodl] * dp[childRight][bodr][j];
            dp[pos][i][j] %= mod;
          }
}
long long getRes() {
  long long res = 0;
  for (int i = 0; i < 3; ++i)
    for (int j = 0; j < 3; ++j) res += dp[0][i][j], res %= mod;
  return res;
}
int main() {
  ios_base::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < 3; ++i)
    for (int j = 0; j < 3; ++j) cin >> good[i][j];
  build(0, 0, n - 1);
  for (int i = 0; i < m; ++i) {
    int v, t;
    cin >> v >> t;
    --v;
    --t;
    update(0, 0, n - 1, v, t);
    cout << getRes() << "\n";
  }
  return 0;
}
