#include <bits/stdc++.h>
using namespace std;
const int mod = 777777777;
const int MAXN = 100 * 1000;
struct node {
  int a[4][4];
  node() { memset((a), 0, sizeof(a)); }
};
node t[4 * MAXN];
int good[4][4];
int len;
node operator+(const node &a, const node &b) {
  node ans;
  for (int i = 1; i <= 3; i++)
    for (int j = 1; j <= 3; j++)
      if (good[i][j])
        for (int l = 1; l <= 3; l++)
          for (int r = 1; r <= 3; r++)
            ans.a[l][r] =
                (int)((ans.a[l][r] + (long long)a.a[l][i] * b.a[j][r]) % mod);
  return ans;
}
void update(int ind, int val) {
  memset((t[ind].a), 0, sizeof(t[ind].a));
  if (val == 0) {
    for (int i = 1; i <= 3; i++) t[ind].a[i][i] = 1;
  } else {
    t[ind].a[val][val] = 1;
  }
  ind /= 2;
  while (ind > 0) {
    t[ind] = t[2 * ind] + t[2 * ind + 1];
    ind /= 2;
  }
}
node get(int tl, int tr, int v, int l, int r) {
  if (tl == l && tr == r) {
    return t[v];
  }
  int s = (l + r) / 2;
  if (s + 1 <= tl) {
    return get(tl, tr, 2 * v + 1, s + 1, r);
  }
  if (tr <= s) {
    return get(tl, tr, 2 * v, l, s);
  }
  return get(tl, s, 2 * v, l, s) + get(s + 1, tr, 2 * v + 1, s + 1, r);
}
int get_sum(const node &a) {
  int ans = 0;
  for (int i = 1; i <= 3; i++)
    for (int j = 1; j <= 3; j++) ans = (ans + a.a[i][j]) % mod;
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= 3; i++)
    for (int j = 1; j <= 3; j++) cin >> good[i][j];
  for (len = 1; len < n; len *= 2)
    ;
  for (int i = 0; i < n; i++) {
    for (int j = 1; j <= 3; j++) t[i + len].a[j][j] = 1;
  }
  for (int i = len - 1; i > 0; i--) {
    t[i] = t[2 * i] + t[2 * i + 1];
  }
  for (int i = 0; i < m; i++) {
    int v, val;
    cin >> v >> val;
    v--;
    update(v + len, val);
    cout << get_sum(get(len, len + n - 1, 1, len, len * 2 - 1)) << '\n';
  }
  return 0;
}
