#include <bits/stdc++.h>
using namespace std;
int N, M, mod = 777777777, W[3][3], C[4 * 77777][3][3];
int sum(int xi) {
  int ret = 0;
  for (int i = 0; i < 3; i++)
    for (int j = 0; j < 3; j++)
      ret = (int)(((long long)ret + C[xi][i][j]) % mod);
  return ret;
}
void mul(int reti, int xi, int yi) {
  for (int i = 0; i < 3; i++)
    for (int j = 0; j < 3; j++) C[reti][i][j] = 0;
  for (int z = 0; z < 3; z++)
    for (int s = 0; s < 3; s++)
      for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
          if (W[i][j] == 1)
            C[reti][s][z] =
                (int)((C[reti][s][z] +
                       ((long long)C[xi][s][i] * C[yi][j][z]) % mod) %
                      mod);
}
void build(int i, int l, int r) {
  if (l == r) {
    for (int x = 0; x < 3; x++) C[i][x][x] = 1;
  } else {
    int md = (l + r) / 2;
    build(2 * i + 1, l, md);
    build(2 * i + 2, md + 1, r);
    mul(i, 2 * i + 1, 2 * i + 2);
  }
}
void update(int i, int l, int r, int v, int t) {
  if (v < l || r < v) return;
  if (l == r) {
    for (int x = 0; x < 3; x++) C[i][x][x] = t == 0 || t == x + 1 ? 1 : 0;
  } else {
    int md = (l + r) / 2;
    update(2 * i + 1, l, md, v, t);
    update(2 * i + 2, md + 1, r, v, t);
    mul(i, 2 * i + 1, 2 * i + 2);
  }
}
int main() {
  cin >> N >> M;
  for (int i = 0; i < 3; i++)
    for (int j = 0; j < 3; j++) cin >> W[i][j];
  build(0, 0, N - 1);
  for (int q = 0; q < M; q++) {
    int v, t;
    cin >> v >> t;
    v--;
    update(0, 0, N - 1, v, t);
    cout << sum(0) << endl;
  }
}
