#include <bits/stdc++.h>
using namespace std;
const int P = 777777777;
int n, m;
int w[4][4];
struct PQ {
  int a[4][4];
  bool f;
  void set(int val) {
    for (int i = 1; i <= 3; i++) {
      for (int j = 1; j <= 3; j++) a[i][j] = 0;
      if (i == val || val == 0) a[i][i] = 1;
    }
  }
  void merge(PQ &p1, PQ &p2) {
    if (!p1.f) {
      *this = p2;
      return;
    }
    if (!p2.f) {
      *this = p1;
      return;
    }
    for (int i = 1; i <= 3; i++) {
      for (int j = 1; j <= 3; j++) a[i][j] = 0;
    }
    for (int i = 1; i <= 3; i++) {
      for (int j = 1; j <= 3; j++) {
        for (int ii = 1; ii <= 3; ii++) {
          for (int jj = 1; jj <= 3; jj++) {
            if (!w[j][ii]) continue;
            a[i][jj] = ((long long)p1.a[i][j] * p2.a[ii][jj] + a[i][jj]) % P;
          }
        }
      }
    }
  }
};
struct Tree {
  static const int N = 1 << 17;
  PQ t[2 * N + 1];
  void build(int num, int tl, int tr, int l, int r);
  void build(int l, int r);
  void update(int num, int tl, int tr, int pos, int val);
  void update(int pos, int val);
  int top();
};
void Tree::build(int num, int tl, int tr, int l, int r) {
  int mid;
  int num1, num2;
  if (tr < l || tl > r) return;
  t[num].f = true;
  if (tl == tr) {
    t[num].set(0);
    return;
  }
  num1 = num * 2;
  num2 = num * 2 + 1;
  mid = (tl + tr) / 2;
  build(num1, tl, mid, l, r);
  build(num2, mid + 1, tr, l, r);
  t[num].merge(t[num1], t[num2]);
}
void Tree::update(int num, int tl, int tr, int pos, int val) {
  int mid;
  int num1, num2;
  if (tl == tr) {
    t[num].set(val);
    return;
  }
  num1 = num * 2;
  num2 = num * 2 + 1;
  mid = (tl + tr) / 2;
  if (pos <= mid) {
    update(num1, tl, mid, pos, val);
  } else {
    update(num2, mid + 1, tr, pos, val);
  }
  t[num].merge(t[num1], t[num2]);
}
void Tree::build(int l, int r) { build(1, 1, N, l, r); }
void Tree::update(int pos, int val) { update(1, 1, N, pos, val); }
int Tree::top() {
  int res = 0;
  for (int i = 1; i <= 3; i++) {
    for (int j = 1; j <= 3; j++) {
      res = (res + t[1].a[i][j]) % P;
    }
  }
  return res;
}
Tree tree;
int main() {
  cin >> n >> m;
  for (int i = 1; i <= 3; i++) {
    for (int j = 1; j <= 3; j++) {
      cin >> w[i][j];
    }
  }
  tree.build(1, n);
  for (int i = 0; i < m; i++) {
    int v, t;
    scanf("%d%d", &v, &t);
    tree.update(v, t);
    printf("%d\n", tree.top());
  }
  return 0;
}
