#include <bits/stdc++.h>
using namespace std;
int w[5][5], nt[77780 * 4][5][5];
void mk(int, int, int);
void updt(int, int, int, int, int);
int main() {
  int i, j, n, q, a, b, ans;
  scanf("%d %d", &n, &q);
  for (i = 1; i <= 3; ++i)
    for (j = 1; j <= 3; ++j) scanf("%d", &w[i][j]);
  mk(1, n, 1);
  while (q--) {
    ans = 0;
    scanf("%d %d", &a, &b);
    updt(a, b, 1, n, 1);
    for (i = 1; i <= 3; ++i)
      for (j = 1; j <= 3; ++j) (ans += nt[1][i][j]) %= 777777777;
    printf("%d\n", ans);
  }
  return 0;
}
void up(int rt) {
  int i, j, p, q;
  for (i = 1; i <= 3; ++i)
    for (j = 1; j <= 3; ++j) nt[rt][i][j] = 0;
  for (i = 1; i <= 3; ++i)
    for (j = 1; j <= 3; ++j)
      if (w[i][j])
        for (p = 1; p <= 3; ++p)
          for (q = 1; q <= 3; ++q)
            (nt[rt][p][q] += 1ll * nt[rt << 1][p][i] * nt[rt << 1 | 1][j][q] %
                             777777777) %= 777777777;
}
void mk(int l, int r, int rt) {
  int m = (l + r) >> 1;
  if (l == r) {
    nt[rt][1][1] = nt[rt][2][2] = nt[rt][3][3] = 1;
    return;
  }
  mk(l, m, rt << 1);
  mk(m + 1, r, rt << 1 | 1);
  up(rt);
}
void updt(int a, int b, int l, int r, int rt) {
  int m = (l + r) >> 1;
  if (l == r) {
    if (!b)
      nt[rt][1][1] = nt[rt][2][2] = nt[rt][3][3] = 1;
    else {
      nt[rt][1][1] = nt[rt][2][2] = nt[rt][3][3] = 0;
      nt[rt][b][b] = 1;
    }
    return;
  }
  if (a <= m)
    updt(a, b, l, m, rt << 1);
  else
    updt(a, b, m + 1, r, rt << 1 | 1);
  up(rt);
}
