#include <bits/stdc++.h>
using namespace std;
const long long N = 100005;
const long long mod = 777777777;
long long a, b, c, d, e, f, g[5][5], h, i, j, k;
struct Segment_Tree {
  struct node {
    long long left, right;
    long long x[5][5];
    friend node operator+(const node &x, const node &y) {
      node ret;
      ret.left = x.left, ret.right = y.right;
      for (long long ii = 1; ii <= 3; ii++)
        for (long long jj = 1; jj <= 3; jj++) {
          ret.x[ii][jj] = 0;
          for (long long cc = 1; cc <= 3; cc++)
            for (long long dd = 1; dd <= 3; dd++)
              if (g[cc][dd])
                (ret.x[ii][jj] += x.x[ii][cc] * y.x[dd][jj]) %= mod;
        }
      return ret;
    }
  };
  node tree[N << 2];
  void Give(long long root, long long vv) {
    memset(tree[root].x, 0, sizeof(tree[root].x));
    for (long long ii = 1; ii <= 3; ii++)
      if (vv == 0 || ii == vv) tree[root].x[ii][ii] = 1;
  }
  void Build(long long root, long long left, long long right) {
    tree[root].left = left, tree[root].right = right;
    if (left == right) {
      Give(root, 0);
      return;
    }
    long long mid = (left + right) >> 1;
    Build(root << 1, left, mid);
    Build(root << 1 | 1, mid + 1, right);
    tree[root] = tree[root << 1] + tree[root << 1 | 1];
  }
  void Change(long long root, long long pos, long long vv) {
    long long mid = (tree[root].left + tree[root].right) >> 1;
    if (tree[root].left == pos && tree[root].right == pos) {
      Give(root, vv);
      return;
    }
    if (pos <= mid)
      Change(root << 1, pos, vv);
    else
      Change(root << 1 | 1, pos, vv);
    tree[root] = tree[root << 1] + tree[root << 1 | 1];
  }
  long long Get() {
    long long ret = 0;
    for (long long ii = 1; ii <= 3; ii++)
      for (long long jj = 1; jj <= 3; jj++) (ret += tree[1].x[ii][jj]) %= mod;
    return ret;
  }
};
Segment_Tree T;
int main() {
  cin >> a >> b;
  for (i = 1; i <= 3; i++)
    for (j = 1; j <= 3; j++) cin >> g[i][j];
  T.Build(1, 1, a);
  for (i = 1; i <= b; i++) {
    cin >> c >> d;
    T.Change(1, c, d);
    cout << T.Get() << endl;
  }
  return 0;
}
