#include <bits/stdc++.h>
using namespace std;
int in[3][3], n, m;
long long tree[78000 * 4][3][3];
void init(int cur, int ll, int rr) {
  if (ll == rr) {
    for (int i = 0; i <= 2; i++) tree[cur][i][i] = 1;
    return;
  }
  int mid = (ll + rr) >> 1;
  init((cur << 1), ll, mid);
  init(((cur << 1) + 1), mid + 1, rr);
  for (int i = 0; i <= 2; i++)
    for (int j = 0; j <= 2; j++)
      for (int k = 0; k <= 2; k++)
        for (int l = 0; l <= 2; l++) {
          tree[cur][i][l] +=
              tree[(cur << 1)][i][j] * tree[((cur << 1) + 1)][k][l] * in[j][k];
          tree[cur][i][l] %= 777777777;
        }
}
void add(int cur, int ll, int rr, int p, int v) {
  memset(tree[cur], 0, sizeof(tree[cur]));
  if (ll == rr) {
    if (!v)
      for (int i = 0; i <= 2; i++) tree[cur][i][i] = 1;
    else
      tree[cur][v - 1][v - 1] = 1;
    return;
  }
  int mid = (ll + rr) >> 1;
  if (p <= mid)
    add((cur << 1), ll, mid, p, v);
  else
    add(((cur << 1) + 1), mid + 1, rr, p, v);
  for (int i = 0; i <= 2; i++)
    for (int j = 0; j <= 2; j++)
      for (int k = 0; k <= 2; k++)
        for (int l = 0; l <= 2; l++) {
          tree[cur][i][l] +=
              tree[(cur << 1)][i][j] * tree[((cur << 1) + 1)][k][l] * in[j][k];
          tree[cur][i][l] %= 777777777;
        }
}
int main() {
  int x, y, ans;
  while (~scanf("%d %d", &n, &m)) {
    memset(tree, 0, sizeof(tree));
    for (int i = 0; i <= 2; i++)
      for (int j = 0; j <= 2; j++) scanf("%d", &in[i][j]);
    init(1, 1, n);
    while (m--) {
      ans = 0;
      scanf("%d %d", &x, &y);
      add(1, 1, n, x, y);
      for (int i = 0; i <= 2; i++)
        for (int j = 0; j <= 2; j++) {
          ans += tree[1][i][j];
          ans %= 777777777;
        }
      printf("%d\n", ans);
    }
  }
  return 0;
}
