#include <bits/stdc++.h>
using namespace std;
class node {
 public:
  int A[4][4];
  node() { memset(A, 0, sizeof A); }
};
int n, m, w[5][5];
node ST[350005], ini[5];
node merge(node L, node R) {
  node nw;
  for (int i = 1; i <= 3; i++)
    for (int j = 1; j <= 3; j++) {
      if (!L.A[i][j]) continue;
      for (int k = 1; k <= 3; k++) {
        if (!w[j][k]) continue;
        for (int l = 1; l <= 3; l++)
          nw.A[i][l] =
              (nw.A[i][l] + (long long)L.A[i][j] * R.A[k][l]) % (777777777);
      }
    }
  return nw;
}
node init(int i, int b, int s) {
  if (b == s) return ST[i] = ini[0];
  int o = (b + s) / 2;
  return ST[i] = merge(init(2 * i, b, o), init(2 * i + 1, o + 1, s));
}
node update(int i, int b, int s, int x, int t) {
  if (x < b || x > s) return ST[i];
  if (b == s) return ST[i] = ini[t];
  int o = (b + s) / 2;
  return ST[i] = merge(update(2 * i, b, o, x, t),
                       update(2 * i + 1, o + 1, s, x, t));
}
int main() {
  int x, t, ans;
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= 3; i++)
    for (int j = 1; j <= 3; j++) scanf("%d", w[i] + j);
  for (int i = 1; i <= 3; i++) ini[0].A[i][i] = 1;
  for (int i = 1; i <= 3; i++) ini[i].A[i][i] = 1;
  init(1, 1, n);
  for (int i = 1; i <= m; i++) {
    scanf("%d %d", &x, &t);
    update(1, 1, n, x, t);
    ans = 0;
    for (int i = 1; i <= 3; i++)
      for (int j = 1; j <= 3; j++) ans = (ans + ST[1].A[i][j]) % (777777777);
    printf("%d\n", ans);
  }
  return 0;
}
