#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:256000000")
using namespace std;
const int INF = 1000 * 1000 * 1000;
const double EPS = 1e-9;
const int MOD = 777777777;
const int MAXN = 77777;
long long tree[3][3][MAXN << 2];
bool w[3][3];
void buildTree(int v, int tl, int tr) {
  if (tl == tr) {
    for (int i = 0; i < 3; ++i) {
      tree[i][i][v] = 1;
    }
  } else {
    int tm = (tl + tr) >> 1;
    buildTree(v << 1, tl, tm);
    buildTree(v << 1 | 1, tm + 1, tr);
    for (int il = 0; il < 3; ++il)
      for (int jl = 0; jl < 3; ++jl)
        for (int ir = 0; ir < 3; ++ir)
          for (int jr = 0; jr < 3; ++jr)
            if (w[jl][ir]) {
              tree[il][jr][v] +=
                  tree[il][jl][v << 1] * tree[ir][jr][v << 1 | 1];
              tree[il][jr][v] %= MOD;
            }
  }
}
void updateTree(int pos, int newVal, int v, int tl, int tr) {
  if (tl == tr) {
    for (int i = 0; i < 3; ++i)
      for (int j = 0; j < 3; ++j) {
        tree[i][j][v] = 0;
      }
    if (newVal == 0) {
      for (int i = 0; i < 3; ++i) {
        tree[i][i][v] = 1;
      }
    } else {
      tree[newVal - 1][newVal - 1][v] = 1;
    }
  } else {
    int tm = (tl + tr) >> 1;
    if (pos <= tm) {
      updateTree(pos, newVal, v << 1, tl, tm);
    } else {
      updateTree(pos, newVal, v << 1 | 1, tm + 1, tr);
    }
    for (int i = 0; i < 3; ++i)
      for (int j = 0; j < 3; ++j) {
        tree[i][j][v] = 0;
      }
    for (int il = 0; il < 3; ++il)
      for (int jl = 0; jl < 3; ++jl)
        for (int ir = 0; ir < 3; ++ir)
          for (int jr = 0; jr < 3; ++jr)
            if (w[jl][ir]) {
              tree[il][jr][v] +=
                  tree[il][jl][v << 1] * tree[ir][jr][v << 1 | 1];
              tree[il][jr][v] %= MOD;
            }
  }
}
int main() {
  int N, M;
  scanf("%d%d", &N, &M);
  for (int i = 0; i < 3; ++i)
    for (int j = 0; j < 3; ++j) {
      scanf("%d", &w[i][j]);
    }
  buildTree(1, 0, N - 1);
  while (M--) {
    int pos, newVal;
    scanf("%d%d", &pos, &newVal);
    updateTree(pos - 1, newVal, 1, 0, N - 1);
    long long res = 0;
    for (int i = 0; i < 3; ++i)
      for (int j = 0; j < 3; ++j) {
        res += tree[i][j][1];
        res %= MOD;
      }
    printf("%I64d\n", res);
  }
  return 0;
}
