#include <bits/stdc++.h>
using namespace std;
const int MOD = 777777777;
vector<int> p[4][4];
char w[4][4];
vector<int> a;
struct item {
  int li, ri, prior, len, prod;
  item *l, *r;
  item() {}
  item(int li, int ri, int prior)
      : li(li), ri(ri), prior(prior), l(NULL), r(NULL) {
    calc();
  }
  void calc() {
    prod = len = p[a[li - 1]][a[ri + 1]][ri - li + 2];
    if (l) prod = (1LL * prod * l->prod) % MOD;
    if (r) prod = (1LL * prod * r->prod) % MOD;
  }
};
typedef item* pitem;
void split(pitem t, int key, pitem& l, pitem& r) {
  if (!t)
    l = r = NULL;
  else if (key < t->li)
    split(t->l, key, l, t->l), r = t;
  else
    split(t->r, key, t->r, r), l = t;
  if (t) t->calc();
}
int ecnt = 0;
void insert(pitem& t, pitem it) {
  ecnt++;
  if (ecnt > 1000) throw;
  if (!t)
    t = it;
  else if (it->prior > t->prior)
    split(t, it->li, it->l, it->r), t = it;
  else
    insert(it->li < t->li ? t->l : t->r, it);
  if (t) t->calc();
  ecnt--;
}
void merge(pitem& t, pitem l, pitem r) {
  if (!l || !r)
    t = l ? l : r;
  else if (l->prior > r->prior)
    merge(l->r, l->r, r), t = l;
  else
    merge(r->l, l, r->l), t = r;
  if (t) t->calc();
}
pitem new_item(int li, int ri, int prior);
pitem erase(pitem& t, int key) {
  if (t->li <= key && key <= t->ri) {
    pitem ans = t;
    merge(t, t->l, t->r);
    if (t) t->calc();
    return ans;
  }
  pitem ans = erase(key < t->li ? t->l : t->r, key);
  if (t) t->calc();
  return ans;
}
const int MAX = 77777 + 10;
pitem pitems[MAX];
item items[MAX];
int cnt_items = 0;
pitem new_item(int li, int ri, int prior) {
  pitem res = pitems[cnt_items++];
  res->l = res->r = NULL;
  res->li = li;
  res->ri = ri;
  res->prior = prior;
  res->calc();
  return res;
}
void del_item(pitem p) { pitems[--cnt_items] = p; }
bool solve() {
  for (int i = 0; i < MAX; ++i) pitems[i] = &items[i];
  int n, m;
  memset(w, 1, sizeof(w));
  if (scanf("%d%d", &n, &m) == EOF) return false;
  for (int i = 1; i < 4; ++i)
    for (int j = 1; j < 4; ++j) {
      int x;
      scanf("%d", &x);
      w[i][j] = x;
    }
  for (int i = 0; i < n + 3; ++i)
    for (int j = 0; j < 4; ++j)
      for (int k = 0; k < 4; ++k) {
        if (i == 0) {
          p[j][k].resize(n + 3);
          p[j][k][0] = j == k;
        } else if (i == 1) {
          p[j][k][1] = w[j][k];
        } else {
          for (int z = 1; z < 4; ++z)
            if (w[z][k]) p[j][k][i] = (p[j][k][i] + p[j][z][i - 1]) % MOD;
        }
      }
  int bad_cnt = 0;
  a.assign(n + 2, 0);
  pitem root = new_item(1, n, rand());
  for (int i = 0; i < m; ++i) {
    int v, t;
    scanf("%d%d", &v, &t);
    bad_cnt -= 2 - w[a[v - 1]][a[v]] - w[a[v]][a[v + 1]];
    int old_a = a[v];
    a[v] = t;
    if (old_a == t) {
    } else if (old_a == 0) {
      pitem cur = erase(root, v);
      if (cur->li < v) insert(root, new_item(cur->li, v - 1, rand()));
      if (cur->ri > v) insert(root, new_item(v + 1, cur->ri, rand()));
      del_item(cur);
    } else if (t) {
      if (v > 1 && a[v - 1] == 0) {
        pitem cur = erase(root, v - 1);
        cur->calc();
        insert(root, new_item(cur->li, cur->ri, rand()));
        del_item(cur);
      }
      if (v < n && a[v + 1] == 0) {
        pitem cur = erase(root, v + 1);
        cur->calc();
        insert(root, new_item(cur->li, cur->ri, rand()));
        del_item(cur);
      }
    } else {
      if (v > 1 && a[v - 1] == 0 && v < n && a[v + 1] == 0) {
        pitem l = erase(root, v - 1);
        pitem r = erase(root, v + 1);
        insert(root, new_item(l->li, r->ri, rand()));
        del_item(l);
        del_item(r);
      } else if (v > 1 && a[v - 1] == 0) {
        pitem l = erase(root, v - 1);
        insert(root, new_item(l->li, v, rand()));
        del_item(l);
      } else if (v < n && a[v + 1] == 0) {
        pitem r = erase(root, v + 1);
        insert(root, new_item(v, r->ri, rand()));
        del_item(r);
      } else
        insert(root, new_item(v, v, rand()));
    }
    bad_cnt += 2 - w[a[v - 1]][a[v]] - w[a[v]][a[v + 1]];
    if (bad_cnt == 0 && root)
      printf("%d\n", root->prod);
    else if (bad_cnt == 0 && !root)
      printf("1\n");
    else
      printf("0\n");
  }
  return true;
}
int main() {
  solve();
  return 0;
}
