#include <bits/stdc++.h>
using namespace std;
const int M = 777777777;
const int N = 78000;
const int K = 4;
const int TSZ = 1 << 17;
int w[K][K];
int t[2 * TSZ][K][K];
int n;
void merge(int x) {
  int l = 2 * x;
  int r = 2 * x + 1;
  for (int i = 0; i < K; ++i)
    for (int j = 0; j < K; ++j) {
      long long p = 0;
      for (int k = 0; k < K; ++k)
        for (int m = 0; m < K; ++m)
          if (w[k][m]) p += (long long)t[l][i][k] * t[r][m][j];
      t[x][i][j] = p % M;
    }
}
void init() {
  for (int i = TSZ; i < TSZ + n; ++i)
    for (int j = 1; j < K; ++j) t[i][j][j] = 1;
  for (int i = TSZ + n; i < 2 * TSZ; ++i) t[i][0][0] = 1;
  for (int i = TSZ - 1; i >= 0; --i) merge(i);
}
void update(int x, int y) {
  x += TSZ;
  for (int i = 0; i < K; ++i)
    for (int j = 0; j < K; ++j) t[x][i][j] = 0;
  if (y == 0) {
    for (int j = 1; j < K; ++j) t[x][j][j] = 1;
  } else
    t[x][y][y] = 1;
  while (x > 1) {
    x /= 2;
    merge(x);
  }
}
int main() {
  int m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < K; ++i) w[i][0] = 1;
  for (int i = 1; i < K; ++i)
    for (int j = 1; j < K; ++j) scanf("%d", w[i] + j);
  init();
  while (m--) {
    int x, y;
    scanf("%d%d", &x, &y);
    --x;
    update(x, y);
    long long res = 0;
    for (int i = 1; i < K; ++i)
      for (int j = 0; j < K; ++j) res += t[1][i][j];
    printf("%d\n", (int)(res % M));
  }
  return 0;
}
