#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
inline void smin(T &a, U b) {
  if (a > b) a = b;
}
template <typename T, typename U>
inline void smax(T &a, U b) {
  if (a < b) a = b;
}
struct Node {
  int st, ed;
  int dp[3][3];
} node[4 * 77777];
int mat[3][3], a[77777];
void push_up(int u) {
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      node[u].dp[i][j] = 0;
      for (int k = 0; k < 3; k++) {
        for (int m = 0; m < 3; m++) {
          if (!mat[k][m]) continue;
          node[u].dp[i][j] += (long long)node[(u << 1)].dp[i][k] *
                              node[((u << 1) + 1)].dp[m][j] % 777777777;
          if (node[u].dp[i][j] >= 777777777) node[u].dp[i][j] -= 777777777;
        }
      }
    }
  }
}
void build(int u, int st, int ed) {
  node[u].st = st;
  node[u].ed = ed;
  if (st + 1 == ed) {
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        node[u].dp[i][j] = (i == j);
      }
    }
    return;
  }
  build((u << 1), st, (st + ed >> 1));
  build(((u << 1) + 1), (st + ed >> 1), ed);
  push_up(u);
}
void update(int u, int pos, int v) {
  if (pos >= node[u].ed) return;
  if (pos < node[u].st) return;
  if (node[u].st + 1 == node[u].ed) {
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        if (v == -1)
          node[u].dp[i][j] = (i == j);
        else
          node[u].dp[i][j] = (i == j && i == v);
      }
    }
    return;
  }
  update((u << 1), pos, v);
  update(((u << 1) + 1), pos, v);
  push_up(u);
}
int calc() {
  int ans = 0;
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      ans += node[1].dp[i][j];
      if (ans >= 777777777) ans -= 777777777;
    }
  }
  return ans;
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      scanf("%d", mat[i] + j);
    }
  }
  build(1, 0, n);
  int v, t;
  for (int i = 0; i < m; i++) {
    scanf("%d %d", &v, &t);
    v--;
    t--;
    update(1, v, t);
    printf("%d\n", calc());
  }
  return 0;
}
