#include <bits/stdc++.h>
using namespace std;
int n, m, A[4][4];
int f[320008][4][4];
const int pp = 777777777;
int size, t1[400008], t2[400008];
struct Tprogram {
  void open() {
    freopen("", "r", stdin);
    freopen("", "w", stdout);
  }
  void close() {
    fclose(stdin);
    fclose(stdout);
  }
  void init() {
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= 3; i++)
      for (int j = 1; j <= 3; j++) scanf("%d", &A[i][j]);
  }
  void add(int &x, int y) {
    x += y;
    if (x >= pp) x -= pp;
  }
  int tmp[4][4];
  void update(int a[4][4], int b[4][4], int c[4][4], int t) {
    for (int i = 1; i <= 3; i++)
      for (int j = 1; j <= 3; j++) a[i][j] = 0;
    if (!t1[t << 1 ^ 1])
      for (int i = 1; i <= 3; i++)
        for (int j = 1; j <= 3; j++) a[i][j] = b[i][j];
    else if (!t2[t << 1])
      for (int i = 1; i <= 3; i++)
        for (int j = 1; j <= 3; j++) a[i][j] = c[i][j];
    else {
      for (int i = 1; i <= 3; i++)
        for (int j = 1; j <= 3; j++) tmp[i][j] = 0;
      for (int j = 1; j <= 3; j++)
        for (int k = 1; k <= 3; k++)
          if (A[j][k])
            for (int l = 1; l <= 3; l++)
              if (c[k][l]) add(tmp[j][l], c[k][l]);
      for (int i = 1; i <= 3; i++)
        for (int j = 1; j <= 3; j++)
          if (b[i][j])
            for (int l = 1; l <= 3; l++)
              add(a[i][l], (long long)b[i][j] * tmp[j][l] % pp);
    }
  }
  void modify(int v, int t) {
    v += size;
    for (int i = 1; i <= 3; i++)
      for (int j = 1; j <= 3; j++) f[v][i][j] = 0;
    if (t == 0)
      for (int i = 1; i <= 3; i++) f[v][i][i] = 1;
    else
      f[v][t][t] = 1;
    for (v >>= 1; v; v >>= 1) update(f[v], f[v << 1], f[v << 1 ^ 1], v);
  }
  void work() {
    size = 1;
    while (size < n + 2) size <<= 1;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= 3; j++)
        f[i + size][j][j] = 1, t1[i + size] = t2[i + size] = 1;
    for (int i = size - 1; i >= 1; i--)
      update(f[i], f[i << 1], f[i << 1 ^ 1], i), t1[i] = t1[i << 1],
                                                 t2[i] = t2[i << 1 ^ 1];
    int v, t;
    for (int i = 1; i <= m; i++) {
      scanf("%d%d", &v, &t);
      int ans = 0;
      modify(v, t);
      for (int i = 1; i <= 3; i++)
        for (int j = 1; j <= 3; j++) add(ans, f[1][i][j]);
      printf("%d\n", ans);
    }
  }
} Program;
int main() {
  Program.init();
  Program.work();
  return 0;
}
