#include <bits/stdc++.h>
using namespace std;
int stree[4 * 77777][3][3], w[3][3], mod = 777777777;
void mul(int p, int l, int r) {
  int i, j, k, l1;
  for (i = 0; i < 3; i++)
    for (j = 0; j < 3; j++) stree[p][i][j] = 0;
  for (i = 0; i < 3; i++)
    for (j = 0; j < 3; j++)
      for (k = 0; k < 3; k++)
        for (l1 = 0; l1 < 3; l1++)
          if (w[k][l1])
            stree[p][i][j] += (1ll * stree[l][i][k] * stree[r][l1][j]) % mod,
                stree[p][i][j] %= mod;
}
void build(int nodeno, int tst, int tnd) {
  if (tst == tnd) {
    for (int i = 0; i < 3; i++) stree[nodeno][i][i] = 1;
    return;
  }
  int mid = (tst + tnd) / 2;
  build(2 * nodeno, tst, mid);
  build(2 * nodeno + 1, mid + 1, tnd);
  mul(nodeno, 2 * nodeno, 2 * nodeno + 1);
}
void update(int nodeno, int tst, int tnd, int idx, int val) {
  if (tst == tnd) {
    for (int i = 0; i < 3; i++)
      stree[nodeno][i][i] = !val || (i + 1) == val ? 1 : 0;
  } else {
    int mid = (tst + tnd) / 2;
    if (idx <= mid)
      update(2 * nodeno, tst, mid, idx, val);
    else
      update(2 * nodeno + 1, mid + 1, tnd, idx, val);
    mul(nodeno, 2 * nodeno, 2 * nodeno + 1);
  }
}
int main() {
  int n, m, i, j, t, u, v;
  cin >> n >> m;
  for (i = 0; i < 3; i++)
    for (j = 0; j < 3; j++) cin >> w[i][j];
  build(1, 0, n - 1);
  while (m--) {
    cin >> v >> t;
    v--;
    update(1, 0, n - 1, v, t);
    int ans = 0;
    for (i = 0; i < 3; i++)
      for (j = 0; j < 3; j++) ans = (ans + stree[1][i][j]) % mod;
    cout << ans << "\n";
  }
}
