#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
const long long mod = 777777777;
int a[N], w[4][4], dp[4 * N][4][4], n;
void build(int l = 0, int r = n, int id = 1) {
  if (r - l == 1) {
    for (int i = 1; i <= 3; i++) dp[id][i][i] = 1;
    return;
  }
  int mid = (l + r) / 2;
  build(l, mid, 2 * id);
  build(mid, r, 2 * id + 1);
  for (int i = 1; i <= 3; i++)
    for (int j = 1; j <= 3; j++)
      if (w[i][j])
        for (int x = 1; x <= 3; x++)
          for (int y = 1; y <= 3; y++)
            dp[id][x][y] = (dp[id][x][y] + 1LL * dp[2 * id][x][i] *
                                               dp[2 * id + 1][j][y] % mod) %
                           mod;
}
void upd(int p, int x, int l = 0, int r = n, int id = 1) {
  if (r - l == 1) {
    a[l] = x;
    for (int i = 1; i <= 3; i++)
      for (int j = 1; j <= 3; j++) dp[id][i][j] = 0;
    if (x)
      dp[id][x][x] = 1;
    else
      for (int i = 1; i <= 3; i++) dp[id][i][i] = 1;
    return;
  }
  int mid = (l + r) / 2;
  if (p < mid)
    upd(p, x, l, mid, 2 * id);
  else
    upd(p, x, mid, r, 2 * id + 1);
  for (int i = 1; i <= 3; i++)
    for (int j = 1; j <= 3; j++) dp[id][i][j] = 0;
  for (int i = 1; i <= 3; i++)
    for (int j = 1; j <= 3; j++)
      if (w[i][j])
        for (int x = 1; x <= 3; x++)
          for (int y = 1; y <= 3; y++)
            dp[id][x][y] = (dp[id][x][y] + 1LL * dp[2 * id][x][i] *
                                               dp[2 * id + 1][j][y] % mod) %
                           mod;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int q;
  cin >> n >> q;
  for (int i = 1; i <= 3; i++)
    for (int j = 1; j <= 3; j++) cin >> w[i][j];
  build();
  while (q--) {
    int p, x, ans = 0;
    cin >> p >> x;
    upd(p - 1, x);
    for (int i = 1; i <= 3; i++)
      for (int j = 1; j <= 3; j++) ans = (ans + dp[1][i][j]) % mod;
    cout << ans << '\n';
  }
  return 0;
}
