#include <bits/stdc++.h>
using namespace std;
long long int powmod(long long int a, int b, int n) {
  long long int rm = 1;
  while (b) {
    if (b % 2) {
      rm = (rm * a) % n;
    }
    a = (a * a) % n;
    b /= 2;
  }
  return rm;
}
int a[int(1e5 + 10)] = {0}, n, Q, v, t, M[4][4];
long long int seg[4 * 77800][4][4] = {0};
void update(int n, int it, int f, int ind, int value) {
  if (it == f) {
    a[ind] = value;
    memset(seg[n], 0, sizeof(seg[n]));
    ;
    if (value) {
      seg[n][value][value] = 1;
    } else {
      for (int i = 1; i < 4; ++i) seg[n][i][i] = 1;
    }
    return;
  }
  int m = (it + f) / 2;
  if (ind <= m) {
    update(n * 2, it, m, ind, value);
  } else {
    update(n * 2 + 1, m + 1, f, ind, value);
  }
  memset(seg[n], 0, sizeof(seg[n]));
  ;
  for (int l1 = 1; l1 <= 3; ++l1) {
    for (int r2 = 1; r2 <= 3; ++r2) {
      for (int r1 = 1; r1 <= 3; ++r1) {
        for (int l2 = 1; l2 <= 3; ++l2) {
          seg[n][l1][r2] =
              (seg[n][l1][r2] +
               seg[n * 2][l1][r1] * seg[n * 2 + 1][l2][r2] * M[r1][l2]) %
              int(777777777);
        }
      }
    }
  }
}
void insert(int n, int it, int f) {
  if (it == f) {
    seg[n][1][1] = seg[n][2][2] = seg[n][3][3] = 1;
    return;
  }
  int m = (it + f) / 2;
  insert(n * 2, it, m);
  insert(n * 2 + 1, m + 1, f);
  for (int l1 = 1; l1 <= 3; ++l1) {
    for (int r2 = 1; r2 <= 3; ++r2) {
      for (int r1 = 1; r1 <= 3; ++r1) {
        for (int l2 = 1; l2 <= 3; ++l2) {
          seg[n][l1][r2] =
              (seg[n][l1][r2] +
               seg[n * 2][l1][r1] * seg[n * 2 + 1][l2][r2] * M[r1][l2]) %
              int(777777777);
        }
      }
    }
  }
}
int main() {
  cin >> n >> Q;
  for (int i = 1; i <= 3; ++i)
    for (int j = 1; j <= 3; ++j) cin >> M[i][j];
  int ans = 0;
  insert(1, 1, n);
  while (Q--) {
    cin >> v >> t;
    ans = 0;
    if (a[v] != t) update(1, 1, n, v, t);
    for (int j = 1; j <= 3; ++j) {
      for (int k = 1; k <= 3; ++k) {
        ans = (ans + seg[1][j][k]) % int(777777777);
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}
