#include <bits/stdc++.h>
using namespace std;
long long base = 777777777, tree[320100][4][4];
int n, m;
int lb[320100], rb[320100];
int mas[4][4];
void modify(int v, int x, int q) {
  if (lb[v] == rb[v]) {
    for (int i = 1; i <= 3; i++)
      for (int j = 1; j <= 3; j++) tree[v][i][j] = 0;
    if (q == 0) {
      for (int i = 1; i <= 3; i++) tree[v][i][i] = 1;
    } else {
      tree[v][q][q] = 1;
    }
    return;
  }
  int med = (lb[v] + rb[v]) / 2;
  if (x <= med) {
    modify(2 * v + 1, x, q);
  } else {
    modify(2 * v + 2, x, q);
  }
  for (int l = 1; l <= 3; l++)
    for (int r = 1; r <= 3; r++) {
      tree[v][l][r] = 0;
      for (int ll = 1; ll <= 3; ll++)
        for (int rr = 1; rr <= 3; rr++) {
          if (mas[ll][rr] == 1) {
            tree[v][l][r] += tree[2 * v + 1][l][ll] * tree[2 * v + 2][rr][r];
            tree[v][l][r] %= base;
          }
        }
    }
  return;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= 3; i++)
    for (int j = 1; j <= 3; j++) scanf("%d", &mas[i][j]);
  lb[0] = 1;
  rb[0] = n;
  for (int i = 1; i <= n * 4 + 100; i++)
    if (i % 2 == 1) {
      lb[i] = lb[i / 2];
      rb[i] = (lb[i / 2] + rb[i / 2]) / 2;
    } else {
      rb[i] = rb[(i - 2) / 2];
      lb[i] = (lb[(i - 2) / 2] + rb[(i - 2) / 2]) / 2 + 1;
    }
  for (int i = 1; i <= n; i++) modify(0, i, 0);
  for (int i = 1; i <= m; i++) {
    int aa, bb;
    scanf("%d%d", &aa, &bb);
    modify(0, aa, bb);
    long long ans = 0;
    for (int l = 1; l <= 3; l++)
      for (int r = 1; r <= 3; r++) ans += tree[0][l][r], ans %= base;
    printf("%I64d\n", ans);
  }
  return 0;
}
