#include <bits/stdc++.h>
using namespace std;
int getint() {
  int ret = 0, mul = 1;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
  if (ch == '-') {
    mul = -1;
    ch = getchar();
  }
  do ret = (ret << 1) + (ret << 3) + ch - '0';
  while ((ch = getchar()) && ('0' <= ch && ch <= '9'));
  return ret * mul;
}
void getint(int &x) { x = getint(); }
long long getLL() {
  long long ret = 0, mul = 1LL;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
  if (ch == '-') {
    mul = -1LL;
    ch = getchar();
  }
  do ret = (ret << 1) + (ret << 3) + ch - '0';
  while ((ch = getchar()) && ('0' <= ch && ch <= '9'));
  return ret * mul;
}
void getLL(long long &x) { x = getLL(); }
struct node {
  long long l, r;
  long long way[4][4];
};
const long long MOD = 777777777LL;
const long long MAX = 6LL * 77777LL;
node nod[MAX];
long long good[4][4], n, m, x, t;
void build(long long v, long long l, long long r) {
  nod[v].l = l;
  nod[v].r = r;
  memset(nod[v].way, 0, sizeof(nod[v].way));
  if (l == r) {
    for (long long i = 1; i <= 3; ++i) nod[v].way[i][i] = 1;
    return;
  }
  build(2 * v, l, (l + r) / 2);
  build(2 * v + 1, (l + r + 2) / 2, r);
  for (long long i = 1; i <= 3; ++i)
    for (long long j = 1; j <= 3; ++j)
      for (long long x = 1; x <= 3; ++x)
        for (long long y = 1; y <= 3; ++y)
          if (good[x][y])
            nod[v].way[i][j] =
                (nod[v].way[i][j] +
                 nod[2 * v].way[i][x] * nod[2 * v + 1].way[y][j]) %
                MOD;
}
void update(long long v, long long x, long long t) {
  if (nod[v].l == x && nod[v].r == x) {
    nod[v].way[1][1] = nod[v].way[2][2] = nod[v].way[3][3] = (t == 0 ? 1 : 0);
    nod[v].way[t][t] = 1;
    return;
  }
  if (nod[v].r < x || nod[v].l > x) return;
  update(2 * v, x, t);
  update(2 * v + 1, x, t);
  for (long long i = 1; i <= 3; ++i)
    for (long long j = 1; j <= 3; ++j) {
      nod[v].way[i][j] = 0;
      for (long long x = 1; x <= 3; ++x)
        for (long long y = 1; y <= 3; ++y)
          if (good[x][y])
            nod[v].way[i][j] =
                (nod[v].way[i][j] +
                 nod[2 * v].way[i][x] * nod[2 * v + 1].way[y][j]) %
                MOD;
    }
}
long long total() {
  long long ret = 0;
  for (long long i = 1; i <= 3; ++i)
    for (long long j = 1; j <= 3; ++j) ret = (ret + nod[1].way[i][j]) % MOD;
  return ret;
}
int main() {
  scanf("%I64d%I64d", &n, &m);
  for (long long i = 1; i <= 3; ++i)
    for (long long j = 1; j <= 3; ++j) scanf("%I64d", &good[i][j]);
  build(1, 1, n);
  for (long long task = 0; task < m; ++task) {
    scanf("%I64d%I64d", &x, &t);
    update(1, x, t);
    printf("%I64d\n", total());
  }
  return 0;
}
