#include <bits/stdc++.h>
using namespace std;
const long long N = 80000, MOD = 777777777;
long long n, q;
long long fsh[4][4];
long long seg[4 * N][4][4];
void build(long long b = 1, long long e = n, long long key = 1) {
  if (b == e) {
    seg[key][1][1] = 1;
    seg[key][2][2] = 1;
    seg[key][3][3] = 1;
    return;
  }
  long long mid = b + e >> 1;
  build(b, mid, key * 2);
  build(mid + 1, e, key * 2 + 1);
  for (long long x = 1; x <= 3; ++x)
    for (long long y = 1; y <= 3; ++y)
      for (long long m1 = 1; m1 <= 3; ++m1)
        for (long long m2 = 1; m2 <= 3; ++m2)
          if (fsh[m1][m2]) {
            seg[key][x][y] += seg[key * 2][x][m1] * seg[key * 2 + 1][m2][y];
            seg[key][x][y] %= MOD;
          }
}
void upd(long long p, long long val, long long b = 1, long long e = n,
         long long key = 1) {
  if (p < b or e < p) return;
  for (long long i = 1; i <= 3; ++i)
    for (long long j = 1; j <= 3; ++j) seg[key][i][j] = 0;
  if (b == e) {
    if (!val) {
      seg[key][1][1] = 1;
      seg[key][2][2] = 1;
      seg[key][3][3] = 1;
    }
    seg[key][val][val] = 1;
    return;
  }
  long long mid = b + e >> 1;
  upd(p, val, b, mid, key * 2);
  upd(p, val, mid + 1, e, key * 2 + 1);
  for (long long x = 1; x <= 3; ++x)
    for (long long y = 1; y <= 3; ++y)
      for (long long m1 = 1; m1 <= 3; ++m1)
        for (long long m2 = 1; m2 <= 3; ++m2)
          if (fsh[m1][m2]) {
            seg[key][x][y] += seg[key * 2][x][m1] * seg[key * 2 + 1][m2][y];
            seg[key][x][y] %= MOD;
          }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> q;
  for (long long i = 1; i <= 3; ++i)
    for (long long j = 1; j <= 3; ++j) cin >> fsh[i][j];
  build();
  for (long long i = 1, p, val, res; i <= q; ++i) {
    res = 0;
    cin >> p >> val;
    upd(p, val);
    for (long long j = 1; j <= 3; ++j)
      for (long long l = 1; l <= 3; ++l) res += seg[1][j][l];
    res %= MOD;
    cout << res << "\n";
  }
  return 0;
}
