#include <bits/stdc++.h>
using namespace std;
const long long mod = 777777777;
const long long inf = 1e16;
int v[78000], n, m;
int w[3][3];
long long dp[4 * 78000][3][3];
inline void combine(int o) {
  for (int i = 0; i < 3; ++i) {
    for (int j = 0; j < 3; ++j) {
      dp[o][i][j] = 0;
    }
  }
  for (int i = 0; i < 3; ++i) {
    for (int j = 0; j < 3; ++j) {
      for (int k = 0; k < 3; ++k) {
        for (int l = 0; l < 3; ++l) {
          if (w[j][k] == 0) continue;
          dp[o][i][l] = (dp[(o << 1)][i][j] * dp[(o << 1 | 1)][k][l] % mod +
                         dp[o][i][l]) %
                        mod;
        }
      }
    }
  }
}
void build(int o, int L, int R) {
  if (L == R) {
    for (int i = 0; i < 3; ++i) {
      dp[o][i][i] = 1;
    }
    return;
  }
  build((o << 1), L, ((L + R) >> 1));
  build((o << 1 | 1), ((L + R) >> 1) + 1, R);
  combine(o);
}
void update(int o, int L, int R, int pos) {
  if (L == R) {
    for (int i = 0; i < 3; ++i) {
      dp[o][i][i] = 0;
    }
    if (v[L]) {
      dp[o][v[L] - 1][v[L] - 1] = 1;
    } else {
      for (int i = 0; i < 3; ++i) {
        dp[o][i][i] = 1;
      }
    }
    return;
  }
  if (pos <= ((L + R) >> 1)) {
    update((o << 1), L, ((L + R) >> 1), pos);
  } else {
    update((o << 1 | 1), ((L + R) >> 1) + 1, R, pos);
  }
  combine(o);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < 3; ++i) {
    for (int j = 0; j < 3; ++j) {
      scanf("%d", &w[i][j]);
    }
  }
  build(1, 1, n);
  while (m--) {
    int a, b;
    scanf("%d%d", &a, &b);
    v[a] = b;
    update(1, 1, n, a);
    long long ret = 0;
    for (int i = 0; i < 3; ++i) {
      for (int j = 0; j < 3; ++j) {
        ret = (ret + dp[1][i][j]) % mod;
      }
    }
    printf("%lld\n", ret);
  }
  return 0;
}
