#include <bits/stdc++.h>
using namespace std;
const int mod = 777777777;
int n, m, mat[4][4], v, t;
struct SEG {
  int l, r;
  long long dp[4][4];
} seg[300000];
void up(int i) {
  memset(seg[i].dp, 0, sizeof(seg[i].dp));
  for (int x = 1; x <= 3; x++)
    for (int y = 1; y <= 3; y++)
      if (mat[x][y]) {
        for (int q = 1; q <= 3; q++)
          for (int w = 1; w <= 3; w++)
            seg[i].dp[q][w] = (seg[i].dp[q][w] +
                               seg[2 * i].dp[q][x] * seg[2 * i + 1].dp[y][w]) %
                              mod;
      }
}
void build(int l, int r, int i) {
  seg[i].l = l;
  seg[i].r = r;
  memset(seg[i].dp, 0, sizeof(seg[i].dp));
  seg[i].dp[1][1] = 1;
  seg[i].dp[2][2] = 1;
  seg[i].dp[3][3] = 1;
  if (l == r) return;
  int m = (l + r) >> 1;
  build(l, m, 2 * i);
  build(m + 1, r, 2 * i + 1);
  up(i);
}
void ud(int x, int i, int t) {
  if (seg[i].l == seg[i].r) {
    memset(seg[i].dp, 0, sizeof(seg[i].dp));
    if (t == 0) {
      seg[i].dp[1][1] = seg[i].dp[2][2] = seg[i].dp[3][3] = 1;
    } else
      seg[i].dp[t][t] = 1;
    return;
  }
  int m = (seg[i].l + seg[i].r) >> 1;
  if (m < x)
    ud(x, 2 * i + 1, t);
  else
    ud(x, 2 * i, t);
  up(i);
}
long long getans() {
  long long sum = 0;
  for (int i = 1; i <= 3; i++)
    for (int j = 1; j <= 3; j++) {
      sum = (sum + seg[1].dp[i][j]) % mod;
    }
  return sum;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= 3; i++)
    for (int j = 1; j <= 3; j++) scanf("%d", &mat[i][j]);
  build(1, n, 1);
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &v, &t);
    ud(v, 1, t);
    cout << getans() << endl;
  }
  return 0;
}
