#include <bits/stdc++.h>
using namespace std;
struct qwe {
  int q1, q2;
  short int q;
  long long int w[4][4];
};
const int d = 77800, o = 777777777;
bool w[4][4];
qwe a[4 * d];
int i, j, n, v, m;
short int t, y;
void obn(int i) {
  int q1 = 2 * i, q2 = q1 + 1, h1, h2, h3, h4;
  for (h1 = 1; h1 < 4; h1++)
    for (h4 = 1; h4 < 4; h4++) {
      long long int q = 0;
      for (h2 = 1; h2 < 4; h2++) {
        for (h3 = 1; h3 < 4; h3++)
          if (w[h2][h3]) {
            q += a[q1].w[h1][h2] * a[q2].w[h3][h4];
          }
      }
      q = q % o;
      a[i].w[h1][h4] = q;
    }
  return;
}
void postr(int i, int q1, int q2) {
  if (q1 != q2) {
    int qq = (q1 + q2) / 2;
    postr(2 * i, q1, qq);
    postr(2 * i + 1, qq + 1, q2);
    obn(i);
    a[i].q1 = q1;
    a[i].q2 = q2;
  } else {
    int h;
    for (h = 1; h < 4; h++) a[i].w[h][h] = 1;
    a[i].q1 = q1;
    a[i].q2 = q2;
  }
  return;
}
void izm(int i) {
  if (a[i].q1 != a[i].q2) {
    int qq = (a[i].q1 + a[i].q2) / 2;
    if (v <= qq)
      izm(2 * i);
    else
      izm(2 * i + 1);
    obn(i);
  } else {
    int h;
    for (h = 1; h < 4; h++)
      if (h == t)
        a[i].w[h][h] = 1;
      else
        a[i].w[h][h] = y;
  }
}
int sum() {
  long long int q = 0;
  int h1, h2;
  for (h1 = 1; h1 < 4; h1++)
    for (h2 = 1; h2 < 4; h2++) {
      q += a[1].w[h1][h2];
    }
  q = q % o;
  int r = q;
  return r;
}
int main() {
  scanf("%d %d", &n, &m);
  for (j = 1; j < 4; j++) {
    for (i = 1; i < 4; i++) scanf("%hd", &w[j][i]);
  }
  postr(1, 1, n);
  for (j = 0; j < m; j++) {
    scanf("%d%hd", &v, &t);
    if (t > 0)
      y = 0;
    else
      y = 1;
    izm(1);
    printf("%d\n", sum());
  }
  return 0;
}
