#include <bits/stdc++.h>
using namespace std;
template <typename T>
T gcd(T a, T b) {
  return b == 0 ? a : gcd(b, a % b);
}
template <typename T>
T lcm(T a, T b) {
  return a / gcd(a, b) * b;
}
template <typename T>
T ext_gcd(T a, T b, T& first, T& second) {
  if (b == 0) {
    first = 1, second = 0;
    return a;
  }
  T d = ext_gcd(b, a % b, second, first);
  second -= first * (a / b);
  return d;
}
template <typename T>
T inverse(T a, T n) {
  T first, second;
  T t = ext_gcd(a, n, first, second);
  if (t != 1) return -1;
  return (first % n + n) % n;
}
template <typename T>
T Q_mul(T a, T b, T Mod) {
  T ret;
  for (ret = 0; b; b >>= 1, a = (a + a) % Mod)
    if (b & 1) ret = (ret + a) % Mod;
  return ret;
}
template <typename T>
T quick_pow_mod(T a, T b, T Mod) {
  T ret;
  for (ret = 1; b; b >>= 1, (a *= a) %= Mod)
    if (b & 1) (ret *= a) %= Mod;
  return ret;
}
template <typename T>
T quick_pow(T a, T b) {
  T ret;
  for (ret = 1; b; b >>= 1, a *= a)
    if (b & 1) ret *= a;
  return ret;
}
double combination(int n, int r) {
  if (r < n - r) return combination(n, n - r);
  double s = 1.0;
  for (int i = 1; i <= int(n - r); ++i) s *= (r + i), s /= i;
  return s;
}
template <typename T>
inline void read(T& v) {
  T ret = 0, sgnv = 1;
  char p;
  while (!isdigit(p = getchar()))
    if (p == '-') sgnv = -1;
  do ret = (ret << 3) + (ret << 1) + p - '0';
  while (isdigit(p = getchar()));
  v = sgnv * ret;
}
const long long mod = 777777777;
long long t[(1 << 18) + 5][3][3];
int legal[3][3];
void build(int l, int r, int k) {
  if (l == r) {
    for (int i = 0; i < int(3); ++i) t[k][i][i] = 1;
    return;
  }
  int mid = (l + r) >> 1, left = k << 1, right = left | 1;
  build(l, mid, left), build(mid + 1, r, right);
  for (int i = 0; i < int(3); ++i)
    for (int j = 0; j < int(3); ++j)
      if (t[left][i][j])
        for (int a = 0; a < int(3); ++a)
          if (legal[j][a])
            for (int b = 0; b < int(3); ++b)
              if (t[right][a][b]) {
                t[k][i][b] += t[left][i][j] * t[right][a][b] % mod;
                if (t[k][i][b] >= mod) t[k][i][b] -= mod;
              }
}
void update(int s, int l, int r, int k, int v) {
  if (l == r) {
    for (int i = 0; i < int(3); ++i) t[k][i][i] = i == v || v == -1 ? 1 : 0;
    return;
  }
  int mid = (l + r) >> 1, left = k << 1, right = left | 1;
  s <= mid ? update(s, l, mid, left, v) : update(s, mid + 1, r, right, v);
  for (int i = 0; i < int(3); ++i)
    for (int j = 0; j < int(3); ++j) t[k][i][j] = 0;
  for (int i = 0; i < int(3); ++i)
    for (int j = 0; j < int(3); ++j)
      if (t[left][i][j])
        for (int a = 0; a < int(3); ++a)
          if (legal[j][a])
            for (int b = 0; b < int(3); ++b)
              if (t[right][a][b]) {
                t[k][i][b] += t[left][i][j] * t[right][a][b] % mod;
                if (t[k][i][b] >= mod) t[k][i][b] -= mod;
              }
}
inline long long query() {
  long long ans = 0;
  for (int i = 0; i < int(3); ++i)
    for (int j = 0; j < int(3); ++j) (ans += t[1][i][j]) %= mod;
  return ans;
}
int main() {
  int n, m, s, v;
  read(n), read(m);
  for (int i = 0; i < int(3); ++i)
    for (int j = 0; j < int(3); ++j) read(legal[i][j]);
  build(1, n, 1);
  while (m--)
    read(s), read(v), update(s, 1, n, 1, v - 1), printf("%I64d", query()),
        putchar('\n');
  return 0;
}
