#include <bits/stdc++.h>
using namespace std;
int n, m, mod = 777777777, a[100010];
struct qq {
  int a[4][4];
} g[4], t[400010];
qq mul(qq a, qq b) {
  qq c;
  memset(c.a, 0, sizeof(c.a));
  for (int k = 1; k <= 3; k++)
    for (int i = 1; i <= 3; i++)
      for (int j = 1; j <= 3; j++)
        (c.a[i][j] += 1ll * a.a[i][k] * b.a[k][j] % mod) %= mod;
  return c;
}
void build(int p, int l, int r) {
  if (l == r) {
    t[p] = g[0];
    return;
  }
  int mid = (l + r) >> 1;
  build(p << 1, l, mid);
  build(p << 1 | 1, mid + 1, r);
  t[p] = mul(t[p << 1], t[p << 1 | 1]);
}
void update(int p, int l, int r, int x, int z) {
  if (l == r) {
    t[p] = g[z];
    return;
  }
  int mid = (l + r) >> 1;
  if (x <= mid)
    update(p << 1, l, mid, x, z);
  else
    update(p << 1 | 1, mid + 1, r, x, z);
  t[p] = mul(t[p << 1], t[p << 1 | 1]);
}
qq ask(int p, int l, int r, int x) {
  if (x == l) return t[p];
  int mid = (l + r) >> 1;
  if (x <= mid) return mul(ask(p << 1, l, mid, x), t[p << 1 | 1]);
  return ask(p << 1 | 1, mid + 1, r, x);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= 3; i++)
    for (int j = 1; j <= 3; j++) scanf("%d", &g[0].a[i][j]);
  for (int i = 1; i <= 3; i++) {
    g[i] = g[0];
    for (int k = 1; k <= 3; k++)
      if (i != k)
        for (int j = 1; j <= 3; j++) g[i].a[j][k] = 0;
  }
  build(1, 1, n);
  for (int i = 0, x, z; i < m; i++) {
    scanf("%d%d", &x, &z);
    a[x] = z;
    update(1, 1, n, x, z);
    qq ans;
    memset(ans.a, 0, sizeof(ans.a));
    for (int j = 1; j <= 3; j++) ans.a[1][j] = !a[1] | a[1] == j;
    if (n != 1) ans = mul(ans, ask(1, 1, n, 2));
    printf("%d\n", ((ans.a[1][1] + ans.a[1][2]) % mod + ans.a[1][3]) % mod);
  }
  return 0;
}
