#include <bits/stdc++.h>
struct abc {
  int l, r;
  long long dp[3][3];
} c[777770];
int a[3][3];
void build(int x, int l, int r) {
  c[x].l = l, c[x].r = r;
  if (l == r) {
    c[x].dp[0][0] = c[x].dp[1][1] = c[x].dp[2][2] = 1;
    return;
  }
  int mid = (l + r) >> 1;
  build((x << 1) + 1, l, mid);
  build((x << 1) + 2, mid + 1, r);
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      c[x].dp[i][j] = 0;
      for (int p = 0; p < 3; p++) {
        for (int q = 0; q < 3; q++) {
          if (a[p][q]) {
            c[x].dp[i][j] = (c[x].dp[i][j] + 1ll * c[(x << 1) + 1].dp[i][p] *
                                                 c[(x << 1) + 2].dp[q][j]) %
                            777777777;
          }
        }
      }
    }
  }
}
void insert(int x, int p, int v) {
  if (c[x].l == c[x].r) {
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        c[x].dp[i][j] = 0;
      }
    }
    if (v >= 0)
      c[x].dp[v][v] = 1;
    else {
      c[x].dp[0][0] = c[x].dp[1][1] = c[x].dp[2][2] = 1;
    }
    return;
  }
  int mid = (c[x].l + c[x].r) >> 1;
  if (p <= mid)
    insert((x << 1) + 1, p, v);
  else
    insert((x << 1) + 2, p, v);
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      c[x].dp[i][j] = 0;
      for (int p = 0; p < 3; p++) {
        for (int q = 0; q < 3; q++) {
          if (a[p][q]) {
            c[x].dp[i][j] = (c[x].dp[i][j] + 1ll * c[(x << 1) + 1].dp[i][p] *
                                                 c[(x << 1) + 2].dp[q][j]) %
                            777777777;
          }
        }
      }
    }
  }
}
long long query() {
  long long ans = 0;
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      ans += c[0].dp[i][j];
    }
  }
  return ans % 777777777;
}
int main() {
  int n, m, i, j, x, y;
  scanf("%d%d", &n, &m);
  for (i = 0; i < 3; i++) {
    for (j = 0; j < 3; j++) {
      scanf("%d", &a[i][j]);
    }
  }
  build(0, 1, n);
  while (m--) {
    scanf("%d%d", &x, &y);
    insert(0, x, y - 1);
    printf("%I64d\n", query());
  }
  return 0;
}
