#include <bits/stdc++.h>
using namespace std;
const int mod = 777777777;
int n, m, ma[4][4];
struct node {
  int l, r;
  long long ma[4][4];
} T[77778 * 4];
void add(int th) {
  int l = th << 1, r = th << 1 | 1;
  for (int i = 1; i <= 3; i++)
    for (int j = 1; j <= 3; j++) {
      T[th].ma[i][j] = 0;
      for (int x = 1; x <= 3; x++)
        for (int y = 1; y <= 3; y++)
          if (ma[x][y] && T[l].ma[i][x] && T[r].ma[y][j]) {
            T[th].ma[i][j] += T[l].ma[i][x] * T[r].ma[y][j];
            T[th].ma[i][j] %= mod;
          }
    }
}
void build(int l, int r, int th) {
  T[th].l = l;
  T[th].r = r;
  if (T[th].l == T[th].r) {
    for (int i = 1; i <= 3; i++) T[th].ma[i][i] = 1;
    return;
  }
  int m = (T[th].l + T[th].r) >> 1;
  build(l, m, th << 1);
  build(m + 1, r, th << 1 | 1);
  add(th);
}
void update(int th, int pos, int val) {
  if (T[th].l == pos && T[th].r == pos) {
    memset(T[th].ma, 0, sizeof(T[th].ma));
    if (val)
      T[th].ma[val][val] = 1;
    else
      for (int i = 1; i <= 3; i++) T[th].ma[i][i] = 1;
    return;
  }
  if (pos <= T[th * 2].r)
    update(th << 1, pos, val);
  else
    update(th << 1 | 1, pos, val);
  add(th);
}
int query() {
  long long ans = 0;
  for (int i = 1; i <= 3; i++)
    for (int j = 1; j <= 3; j++) ans += T[1].ma[i][j];
  return ans % mod;
}
int main() {
  int x, y;
  cin >> n >> m;
  for (int i = 1; i <= 3; i++)
    for (int j = 1; j <= 3; j++) cin >> ma[i][j];
  build(1, n, 1);
  while (m--) {
    cin >> x >> y;
    update(1, x, y);
    cout << query() << endl;
  }
  return 0;
}
