#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline T qmin(const T a, const T b) {
  return a < b ? a : b;
}
template <typename T>
inline T qmax(const T a, const T b) {
  return a > b ? a : b;
}
template <typename T>
inline void getmin(T &a, const T b) {
  if (a > b) a = b;
}
template <typename T>
inline void getmax(T &a, const T b) {
  if (a < b) a = b;
}
inline void fileio(string s) {
  freopen((s + ".in").c_str(), "r", stdin);
  freopen((s + ".out").c_str(), "w", stdout);
}
const int inf = 1e9 + 7;
const long long linf = 1e17 + 7;
const int N = 1e5 + 7, Node = N << 2;
const int mod = 777777777;
struct mat {
  int val[4][4];
  mat() { memset(val, 0, sizeof(val)); }
  int *operator[](const int &x) { return val[x]; }
  mat operator*(const mat &b) {
    mat ans;
    for (int i = 0; i < 4; ++i) {
      for (int j = 0; j < 4; ++j) {
        long long tot = 0;
        for (int k = 0; k < 4; ++k) tot += (long long)val[i][k] * b.val[k][j];
        ans[i][j] = tot % mod;
      }
    }
    return ans;
  }
} v[Node], w;
int n, q;
inline void init(int u, int l, int r) {
  if (l == r) {
    v[u] = w;
  } else {
    int mid = l + r >> 1;
    init(((u) << 1), l, mid);
    init(((u) << 1 ^ 1), mid + 1, r);
    v[u] = v[((u) << 1)] * v[((u) << 1 ^ 1)];
  }
}
inline void change(int u, int l, int r, int p, int c) {
  if (l == r) {
    if (!c) {
      v[u] = w;
      return;
    }
    for (int i = 0; i < 4; ++i) {
      for (int j = 0; j < 4; ++j) {
        v[u][i][j] = (j == c ? w[i][j] : 0);
      }
    }
  } else {
    int mid = l + r >> 1;
    if (p <= mid)
      change(((u) << 1), l, mid, p, c);
    else
      change(((u) << 1 ^ 1), mid + 1, r, p, c);
    v[u] = v[((u) << 1)] * v[((u) << 1 ^ 1)];
  }
}
int main() {
  scanf("%d%d", &n, &q);
  for (int i = 1; i < 4; ++i) {
    w[0][i] = 1;
    for (int j = 1; j < 4; ++j) {
      scanf("%d", w[i] + j);
    }
  }
  init(1, 1, n);
  while (q--) {
    int p, c;
    scanf("%d%d", &p, &c);
    change(1, 1, n, p, c);
    long long ans = 0;
    for (int i = 1; i < 4; ++i) ans += v[1][0][i];
    printf("%d\n", ans % mod);
  }
  return 0;
}
