#include <bits/stdc++.h>
using namespace std;
template <class T, class U>
inline void chmin(T &t, U f) {
  if (t > f) t = f;
}
template <class T, class U>
inline void chmax(T &t, U f) {
  if (t < f) t = f;
}
const long long mod = 777777777;
long long N, M;
long long T[3][3];
const long long SEG = 1 << 17;
long long seg[SEG * 2][3][3];
inline void update_node(long long k) {
  if (seg[k * 2 + 2][0][0] == -1) {
    for (long long i = 0; i < (3); i++)
      for (long long j = 0; j < (3); j++) seg[k][i][j] = seg[k * 2 + 1][i][j];
    return;
  }
  for (long long i = 0; i < (3); i++)
    for (long long j = 0; j < (3); j++) {
      seg[k][i][j] = 0;
      for (long long ii = 0; ii < (3); ii++)
        for (long long jj = 0; jj < (3); jj++) {
          if (!T[ii][jj]) continue;
          seg[k][i][j] =
              (seg[k][i][j] + seg[k * 2 + 1][i][ii] * seg[k * 2 + 2][jj][j]) %
              mod;
        }
    }
}
void init() {
  memset(seg, 0, sizeof(seg));
  for (long long i = 0; i < (N); i++)
    for (long long j = 0; j < (3); j++) seg[i + SEG - 1][j][j] = 1;
  for (long long i = (N); i < (SEG); i++) seg[i + SEG - 1][0][0] = -1;
  for (long long i = SEG - 2; i >= 0; i--) update_node(i);
}
void update(long long k, long long t) {
  k += SEG - 1;
  for (long long i = 0; i < (3); i++)
    for (long long j = 0; j < (3); j++) seg[k][i][j] = 0;
  if (t == 0) {
    for (long long i = 0; i < (3); i++) seg[k][i][i] = 1;
  } else
    seg[k][t - 1][t - 1] = 1;
  while (k) {
    k = (k - 1) / 2;
    update_node(k);
  }
}
signed main() {
  scanf("%lld%lld", &N, &M);
  for (long long i = 0; i < (3); i++)
    for (long long j = 0; j < (3); j++) scanf("%lld", &T[i][j]);
  init();
  while (M--) {
    long long k, t;
    scanf("%lld%lld", &k, &t);
    update(k - 1, t);
    long long sum = 0;
    for (long long i = 0; i < (3); i++)
      for (long long j = 0; j < (3); j++) sum += seg[0][i][j];
    printf("%lld\n", sum % mod);
  }
  return 0;
}
