#include <bits/stdc++.h>
using namespace std;
const int maxn = 77777 + 1, mod = 777777777;
int n, Q;
vector<pair<int, int> > good;
struct node {
  long long dp[4][4];
  node() {
    for (int i = 0; i < 4; i++) {
      for (int j = 0; j < 4; j++) {
        dp[i][j] = 0;
      }
    }
  }
};
node seg[4 * maxn];
node Merge(node a, node b) {
  node res;
  for (auto tmp : good) {
    for (int i = 1; i <= 3; i++) {
      for (int j = 1; j <= 3; j++) {
        res.dp[i][j] =
            (res.dp[i][j] + (a.dp[i][tmp.first] * b.dp[tmp.second][j])) % mod;
      }
    }
  }
  return res;
}
void build(int id = 1, int L = 1, int R = n + 1) {
  if (L == R - 1) {
    seg[id].dp[1][1] = 1;
    seg[id].dp[2][2] = 1;
    seg[id].dp[3][3] = 1;
    return;
  }
  int mid = (L + R) / 2;
  build(id * 2, L, mid);
  build(id * 2 + 1, mid, R);
  seg[id] = Merge(seg[id * 2], seg[id * 2 + 1]);
  return;
}
void upd(int x, int y, int id = 1, int L = 1, int R = n + 1) {
  if (L == R - 1) {
    if (y == 0) {
      seg[id].dp[1][1] = 1;
      seg[id].dp[2][2] = 1;
      seg[id].dp[3][3] = 1;
      return;
    }
    seg[id].dp[1][1] = 0;
    seg[id].dp[2][2] = 0;
    seg[id].dp[3][3] = 0;
    seg[id].dp[y][y] = 1;
    return;
  }
  int mid = (L + R) / 2;
  if (x < mid) {
    upd(x, y, id * 2, L, mid);
  } else {
    upd(x, y, id * 2 + 1, mid, R);
  }
  seg[id] = Merge(seg[id * 2], seg[id * 2 + 1]);
  return;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> Q;
  for (int i = 1; i <= 3; i++) {
    for (int j = 1; j <= 3; j++) {
      int tmp;
      cin >> tmp;
      if (tmp == 1) {
        good.push_back({i, j});
      }
    }
  }
  build();
  for (int i = 0; i < Q; i++) {
    int tmp1, tmp2;
    cin >> tmp1 >> tmp2;
    upd(tmp1, tmp2);
    int res = 0;
    for (int i = 1; i <= 3; i++) {
      for (int j = 1; j <= 3; j++) {
        res = (res + seg[1].dp[i][j]) % mod;
      }
    }
    cout << res << endl;
  }
  return 0;
}
