#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3fffffff;
const int SINF = 0x7fffffff;
const long long LINF = 0x3fffffffffffffff;
const long long SLINF = 0x7fffffffffffffff;
const int MAXN = 80007;
const int MAX = 3;
const int MOD = 777777777;
const int MAXS = MAXN * 4;
const int MAXL = 131072;
const int TP = 4;
struct mT {
  mT() { clear(); }
  void clear() { memset(a, 0, sizeof(a)); }
  void setunit() {
    clear();
    for (int i = 0; i < MAX; ++i) a[i][i] = 1;
  }
  mT operator*(const mT &x) const {
    mT ans;
    for (int i = 0; i < MAX; ++i) {
      for (int j = 0; j < MAX; ++j) {
        for (int k = 0; k < MAX; ++k) ans.a[i][j] += a[i][k] * x.a[k][j];
        ans.a[i][j] %= MOD;
      }
    }
    return ans;
  }
  void operator*=(const mT &x) { (*this) = (*this) * x; }
  long long a[MAX][MAX];
};
class stT {
 public:
  stT() {
    for (int i = 0; i < MAXS; ++i) data[i].setunit();
  }
  void upd(int p, mT &x) {
    p += MAXL - 1;
    data[p] = x;
    for (p >>= 1; p; p >>= 1) data[p] = data[p << 1 | 1] * data[p << 1];
  }
  mT query() { return data[1]; }

 private:
  mT data[MAXS];
} st;
int n, m, fp;
mT mat[TP];
int v, t;
void init();
void input();
void inputq();
void work();
int main() {
  init();
  input();
  work();
}
void init() { ios::sync_with_stdio(false); }
void input() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < MAX; ++i)
    for (int j = 0; j < MAX; ++j) scanf("%d", &mat[0].a[j][i]);
}
void inputq() { scanf("%d%d", &v, &t); }
void work() {
  for (int i = 1; i < TP; ++i) {
    for (int j = 0; j < MAX; ++j)
      for (int k = 0; k < MAX; ++k)
        if (j == i - 1) mat[i].a[j][k] = mat[0].a[j][k];
  }
  for (int i = 1; i < n; ++i) st.upd(i, mat[0]);
  fp = 0;
  int cc;
  mT now;
  for (int i = 0; i < m; ++i) {
    inputq();
    --v;
    if (v) {
      st.upd(v, mat[t]);
    } else
      fp = t;
    now.clear();
    if (fp)
      now.a[fp - 1][0] = 1;
    else
      for (int i = 0; i < MAX; ++i) now.a[i][0] = 1;
    now = st.query() * now;
    cc = 0;
    for (int i = 0; i < MAX; ++i) (cc += now.a[i][0]) %= MOD;
    printf("%d\n", cc);
  }
}
