#include <bits/stdc++.h>
using namespace std;
const long long int maxn = (1e5) + 10, mod = 777777777;
long long int n, m, a[maxn], nxt[4], bef[4], mat[4][4];
vector<pair<long long int, long long int> > pi;
long long int seg[4 * maxn][4][4];
void mer(long long int id, long long int s, long long int e) {
  long long int mid = (s + e) >> 1;
  long long int ans = 1;
  for (long long int i = 1; i <= 3; i++)
    for (long long int j = 1; j <= 3; j++) seg[id][i][j] = 0;
  for (long long int i = 1; i <= 3; i++) {
    for (long long int j = 1; j <= 3; j++) {
      for (auto u : pi) {
        if (a[s] && a[s] != i) continue;
        if (a[e - 1] && a[e - 1] != j) continue;
        if (a[mid - 1] && a[mid - 1] != u.first)
          ;
        if (a[mid] && a[mid] != u.second) continue;
        seg[id][i][j] +=
            (seg[2 * id][i][u.first] * seg[2 * id + 1][u.second][j]) % mod;
        seg[id][i][j] %= mod;
      }
    }
  }
}
void build(long long int s = 0, long long int e = n, long long int id = 1) {
  if (s == e - 1) {
    for (long long int i = 1; i <= 3; i++) seg[id][i][i] = 1;
    return;
  }
  long long int mid = (s + e) >> 1;
  build(s, mid, 2 * id);
  build(mid, e, 2 * id + 1);
  mer(id, s, e);
}
void upd(long long int pnt, long long int num, long long int s = 0,
         long long int e = n, long long int id = 1) {
  if (s == e - 1) {
    a[pnt] = num;
    for (long long int i = 1; i <= 3; i++) seg[id][i][i] = 0;
    seg[id][num][num] = 1;
    if (!num)
      for (long long int i = 1; i <= 3; i++) seg[id][i][i] = 1;
    return;
  }
  long long int mid = (s + e) >> 1;
  if (pnt < mid)
    upd(pnt, num, s, mid, 2 * id);
  else
    upd(pnt, num, mid, e, 2 * id + 1);
  mer(id, s, e);
}
long long int get() {
  long long int ans = 0;
  for (long long int i = 1; i <= 3; i++)
    for (long long int j = 1; j <= 3; j++) {
      if (a[0] && a[0] != i) continue;
      if (a[n - 1] && a[n - 1] != j) continue;
      ans += seg[1][i][j];
      ans %= mod;
    }
  return ans;
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> m;
  for (long long int i = 1; i <= 3; i++) {
    for (long long int j = 1; j <= 3; j++) {
      long long int r;
      cin >> r;
      mat[i][j] = r;
      if (mat[i][j]) pi.push_back(make_pair(i, j));
    }
  }
  build();
  while (m--) {
    long long int v, t;
    cin >> v >> t;
    v--;
    upd(v, t);
    long long int ans = get();
    cout << ans << '\n';
  }
  return 0;
}
