#include <bits/stdc++.h>
using namespace std;
int c[78000 * 4][3][3], n, m, g[3][3], q[78000];
void build(int id, int l, int r) {
  if (l == r) {
    c[id][0][0] = c[id][1][1] = c[id][2][2] = 1;
    return;
  }
  int md = (l + r) >> 1;
  build(id << 1, l, md);
  build(id << 1 | 1, md + 1, r);
  for (int i = 0; i < 3; i++)
    for (int j = 0; j < 3; j++) {
      for (int k = 0; k < 3; k++)
        for (int h = 0; h < 3; h++) {
          if (!g[k][h]) continue;
          c[id][i][j] +=
              c[id << 1][i][k] * 1LL * c[id << 1 | 1][h][j] % 777777777;
          if (c[id][i][j] > 777777777) c[id][i][j] %= 777777777;
        }
    }
}
void update(int id, int l, int r, int pos, int x) {
  if (l == r) {
    for (int i = 0; i < 3; i++) c[id][i][i] = ((x == -1) || (i == x));
    return;
  }
  int md = (l + r) >> 1;
  if (pos <= md)
    update(id << 1, l, md, pos, x);
  else
    update(id << 1 | 1, md + 1, r, pos, x);
  for (int i = 0; i < 3; i++)
    for (int j = 0; j < 3; j++) {
      c[id][i][j] = 0;
      for (int k = 0; k < 3; k++)
        for (int h = 0; h < 3; h++) {
          if (!g[k][h]) continue;
          c[id][i][j] +=
              c[id << 1][i][k] * 1LL * c[id << 1 | 1][h][j] % 777777777;
          if (c[id][i][j] > 777777777) c[id][i][j] %= 777777777;
        }
    }
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < 3; i++)
    for (int j = 0; j < 3; j++) cin >> g[i][j];
  build(1, 1, n);
  while (m--) {
    int v, t;
    cin >> v >> t;
    t--;
    update(1, 1, n, v, t);
    int res = 0;
    for (int i = 0; i < 3; i++)
      for (int j = 0; j < 3; j++) {
        res = res + c[1][i][j];
        if (res >= 777777777) res %= 777777777;
      }
    cout << res << endl;
  }
  return 0;
}
