#include <bits/stdc++.h>
using namespace std;
static const int INF = 500000000;
template <class T>
void debug(T a, T b) {
  for (; a != b; ++a) cerr << *a << ' ';
  cerr << endl;
}
const long long int mod = 777777777;
int ok[3][3];
struct segtree {
  struct node {
    long long int a[3][3];
    node() { memset(a, 0, sizeof(a)); }
  };
  node res;
  node merge(node& a, node& b) {
    res = node();
    for (int i = 0; i < 3; ++i)
      for (int j = 0; j < 3; ++j)
        if (a.a[i][j])
          for (int k = 0; k < 3; ++k)
            if (ok[j][k])
              for (int l = 0; l < 3; ++l)
                if (b.a[k][l])
                  res.a[i][l] = (res.a[i][l] + a.a[i][j] * b.a[k][l]) % mod;
    return res;
  }
  node val[400005];
  int n;
  void init(int n_) {
    n = 1;
    while (n < n_) n <<= 1;
    for (int i = 0; i < n_; ++i) {
      for (int t = 0; t < 3; ++t) val[i + n - 1].a[t][t] = 1;
    }
    for (int i = n - 2; i >= 0; --i) {
      val[i] = merge(val[i * 2 + 1], val[i * 2 + 2]);
    }
  }
  void set(int k, int a) {
    k += n - 1;
    val[k] = node();
    if (a >= 0)
      val[k].a[a][a] = 1;
    else
      for (int i = 0; i < 3; ++i) val[k].a[i][i] = 1;
    while (k > 0) {
      k = (k - 1) / 2;
      val[k] = merge(val[k * 2 + 1], val[k * 2 + 2]);
    }
  }
  node tmp[20];
  int cnt;
  void query(int a, int b, int i, int l, int r) {
    if (a <= l && r <= b) {
      tmp[cnt++] = val[i];
      return;
    }
    if (b <= l || r <= a) return;
    int md = (l + r) >> 1;
    query(a, b, i * 2 + 1, l, md);
    query(a, b, i * 2 + 2, md, r);
  }
  int q(int n2) {
    cnt = 0;
    query(0, n2, 0, 0, n);
    for (int i = 0; i < cnt - 1; ++i) tmp[i + 1] = merge(tmp[i], tmp[i + 1]);
    int res = 0;
    for (int i = 0; i < 3; ++i)
      for (int j = 0; j < 3; ++j) res = (res + tmp[cnt - 1].a[i][j]) % mod;
    return res;
  }
};
segtree seg;
int n, m;
int main() {
  cin >> n >> m;
  for (int i = 0; i < 3; ++i)
    for (int j = 0; j < 3; ++j) cin >> ok[i][j];
  seg.init(n);
  for (int hoge = 0; hoge < m; ++hoge) {
    int v, t;
    cin >> v >> t;
    --v;
    --t;
    seg.set(v, t);
    cout << seg.q(n) << endl;
  }
  return 0;
}
