#include <bits/stdc++.h>
using namespace std;
const int N = 87777;
const int MOD = 777777777;
int n, m, f[4][4], aint[4 * N][4][4], val[N];
void update(int nod, int pozx, int pozy, int poz) {
  int i, j, k;
  if (pozx == pozy) {
    for (i = 1; i <= 3; ++i)
      for (j = 1; j <= 3; ++j) aint[nod][i][j] = 0;
    if (val[poz] == 0) {
      for (i = 1; i <= 3; ++i)
        for (j = 1; j <= 3; ++j) aint[nod][i][j] = f[i][j];
      return;
    }
    for (i = 1; i <= 3; ++i) aint[nod][i][val[poz]] = f[i][val[poz]];
    return;
  }
  int mid = (pozx + pozy) / 2;
  if (mid >= poz)
    update(2 * nod, pozx, mid, poz);
  else
    update(2 * nod + 1, mid + 1, pozy, poz);
  for (i = 1; i <= 3; ++i)
    for (j = 1; j <= 3; ++j) {
      aint[nod][i][j] = 0;
      for (k = 1; k <= 3; ++k)
        aint[nod][i][j] =
            (1LL * aint[nod][i][j] +
             1LL * aint[2 * nod][i][k] * aint[2 * nod + 1][k][j]) %
            MOD;
    }
}
int main() {
  int i, j, poz, va;
  cin >> n >> m;
  for (i = 1; i <= 3; ++i)
    for (j = 1; j <= 3; ++j) cin >> f[i][j];
  for (i = 1; i != n; ++i) update(1, 1, n - 1, i);
  for (i = 1; i <= m; ++i) {
    cin >> poz >> va;
    --poz;
    val[poz] = va;
    if (n == 1) {
      if (val[0])
        cout << "1\n";
      else
        cout << "3\n";
      continue;
    }
    if (poz) update(1, 1, n - 1, poz);
    int rez = 0;
    if (!val[0]) {
      for (int j = 1; j <= 3; ++j)
        for (int k = 1; k <= 3; ++k) rez = (rez + aint[1][j][k]) % MOD;
    } else {
      for (int j = 1; j <= 3; ++j) rez = (rez + aint[1][val[0]][j]) % MOD;
    }
    cout << rez << "\n";
  }
  return 0;
}
