#include <bits/stdc++.h>
using namespace std;
const int N = 77777 + 10, MOD = 777777777, M = 5;
long long seg[4 * N], a[N], dp[M][M][N];
int n;
void upd(int x, int val, int s = 0, int e = n, int id = 1) {
  if (x < s || x >= e) return;
  if (e - s == 1) {
    seg[id] = val;
    return;
  }
  int mid = (s + e) / 2;
  upd(x, val, s, mid, 2 * id);
  upd(x, val, mid, e, 2 * id + 1);
  seg[id] = (seg[2 * id] * seg[2 * id + 1]) % MOD;
}
long long get(int l, int r, int s = 0, int e = n, int id = 1) {
  if (r <= s || e <= l) return 1;
  if (l <= s && e <= r) return seg[id];
  int mid = (s + e) / 2;
  return (get(l, r, s, mid, 2 * id) * get(l, r, mid, e, 2 * id + 1)) % MOD;
}
int main() {
  int m;
  cin >> n >> m;
  n++;
  for (int i = 1; i <= 3; i++) {
    for (int j = 1; j <= 3; j++) {
      int d;
      cin >> d;
      dp[i][j][0] = d;
    }
    dp[4][i][0] = dp[i][4][0] = 1;
  }
  dp[4][4][0] = 1;
  for (int l = 1; l <= n; l++)
    for (int i = 0; i < M; i++)
      for (int j = 0; j < M; j++)
        for (int k = 1; k <= 3; k++) {
          dp[i][j][l] += (dp[k][j][l - 1] * dp[i][k][0]) % MOD;
          dp[i][j][l] %= MOD;
        }
  for (int i = 0; i <= n; i++) upd(i, 1);
  set<int> st;
  st.insert(0);
  st.insert(n);
  a[0] = 4;
  a[n] = 4;
  upd(0, dp[4][4][n - 1]);
  while (m--) {
    int id, v;
    cin >> id >> v;
    if (v == 0) {
      if (a[id] == 0) {
        cout << get(0, n) << "\n";
        continue;
      }
      auto L = st.lower_bound(id);
      L--;
      auto R = st.upper_bound(id);
      int l = *L, r = *R;
      int t1 = a[l], t2 = a[r];
      a[id] = v;
      upd(l, dp[t1][t2][r - l - 1]);
      upd(id, 1);
      st.erase(id);
    } else {
      auto L = st.lower_bound(id);
      L--;
      auto R = st.upper_bound(id);
      int l = *L, r = *R;
      int t1 = a[l], t2 = a[r];
      a[id] = v;
      upd(id, dp[v][t2][r - id - 1]);
      upd(l, dp[t1][v][id - l - 1]);
      st.insert(id);
    }
    cout << get(0, n) << "\n";
  }
}
