#include <bits/stdc++.h>
using namespace std;
int n, m, A[4][4], vi, ti;
long long T[265000][4][4];
void update(int node, int start, int end, int l, int r, long long int val) {
  if (start > end || start > r || end < l) return;
  if (start >= l && end <= r) {
    for (int i = 1; i <= 3; i++) {
      for (int j = 1; j <= 3; j++) {
        if (i != j)
          T[node][i][j] = 0;
        else {
          if (i == val || val == 0)
            T[node][i][j] = 1;
          else
            T[node][i][j] = 0;
        }
      }
    }
    return;
  }
  update(node * 2, start, (start + end) / 2, l, r, val),
      update(node * 2 + 1, (start + end) / 2 + 1, end, l, r, val);
  for (int l1 = 1; l1 <= 3; l1++) {
    for (int r2 = 1; r2 <= 3; r2++) {
      T[node][l1][r2] = 0;
      for (int l2 = 1; l2 <= 3; l2++) {
        for (int r1 = 1; r1 <= 3; r1++) {
          if (A[r1][l2] == 0) continue;
          T[node][l1][r2] += T[node * 2][l1][r1] * T[node * 2 + 1][l2][r2];
        }
      }
      T[node][l1][r2] %= 777777777;
    }
  }
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= 3; i++) {
    for (int j = 1; j <= 3; j++) {
      scanf("%d", &A[i][j]);
    }
  }
  for (int i = 1; i <= n; i++) {
    update(1, 1, n, i, i, 0);
  }
  for (int i = 1; i <= m; i++) {
    scanf("%d %d", &vi, &ti);
    update(1, 1, n, vi, vi, ti);
    long long cev = 0;
    for (int j = 1; j <= 3; j++) {
      for (int k = 1; k <= 3; k++) {
        cev = (cev + T[1][j][k]) % 777777777;
      }
    }
    printf("%lld\n", cev);
  }
  return 0;
}
