#include <bits/stdc++.h>
using namespace std;
const int mod = 777777777, maxn = 78 * 1000 + 10;
int n, m, v, t, zero, a[maxn];
vector<pair<int, int> > vec;
bool b;
long long dp[4 * maxn][3][3], ans;
void bld(int x, int &l, int &r) {
  if (r - l == 1) {
    dp[x][0][0] = dp[x][1][1] = dp[x][2][2] = 1;
    return;
  }
  int mid = (l + r) / 2;
  bld(2 * x + 1, l, mid);
  bld(2 * x + 2, mid, r);
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      for (int k = 0; k < ((int(vec.size()))); k++)
        dp[x][i][j] = (dp[x][i][j] + ((dp[2 * x + 1][i][vec[k].first] *
                                       dp[2 * x + 2][vec[k].second][j]) %
                                      mod)) %
                      mod;
    }
  }
}
void cng(int x, int &l, int &r) {
  if (r - l == 1) {
    if (a[l] == 0)
      dp[x][0][0] = dp[x][1][1] = dp[x][2][2] = 0;
    else
      dp[x][a[l] - 1][a[l] - 1] = 0;
    if (t == 0)
      dp[x][0][0] = dp[x][1][1] = dp[x][2][2] = 1;
    else
      dp[x][t - 1][t - 1] = 1;
    a[l] = t;
    return;
  }
  int mid = (l + r) / 2;
  if (v < mid)
    cng(2 * x + 1, l, mid);
  else
    cng(2 * x + 2, mid, r);
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      dp[x][i][j] = 0;
      for (int k = 0; k < ((int(vec.size()))); k++)
        dp[x][i][j] = (dp[x][i][j] + ((dp[2 * x + 1][i][vec[k].first] *
                                       dp[2 * x + 2][vec[k].second][j]) %
                                      mod)) %
                      mod;
    }
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> b;
      if (b) vec.push_back({i, j});
    }
  }
  bld(zero, zero, n);
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) ans = (ans + dp[0][i][j]) % mod;
  }
  for (int i = 0; i < m; i++) {
    cin >> v >> t;
    v--;
    if (a[v] == t) {
      cout << ans << endl;
      continue;
    }
    cng(zero, zero, n);
    ans = 0;
    for (int j = 0; j < 3; j++) {
      for (int k = 0; k < 3; k++) ans = (ans + dp[0][j][k]) % mod;
    }
    cout << ans << endl;
  }
  return 0;
}
