Problem p01511 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

struct I {
  I() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
} init;
typedef long long LL;
const LL MOD = 1e9 + 9;
typedef vector<LL> V;
typedef vector<V> M;

LL d[70][100][100];
void pow_init(int n) {
  for (int b = 1; b < 70; b++) {
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++) {
        d[b][i][j] = 0;
        for (int k = 0; k < n; k++)
          d[b][i][j] += d[b - 1][i][k] * d[b - 1][k][j] % MOD;
        d[b][i][j] %= MOD;
      }
  }
}
M o(int n) { return M(n, V(n, 0)); }
M e(int n) {
  M ret = o(n);
  for (int i = 0; i < n; i++)
    ret[i][i] = 1;
  return ret;
}
M mul(M a, M b) {
  int n = a.size();
  M c = o(n);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < n; k++)
        c[i][j] += a[i][k] * b[k][j] % MOD;
      c[i][j] %= MOD;
    }
  return c;
}
M mul(M a, int m) {
  int n = a.size();
  M c = o(n);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < n; k++)
        c[i][j] += a[i][k] * d[m][k][j] % MOD;
      c[i][j] %= MOD;
    }
  return c;
}
M mat_pow(int m, LL n) {
  M ret = e(m);
  int i = 0;
  while (n) {
    if (n & 1)
      ret = mul(ret, i);
    n >>= 1;
    i++;
  }
  return ret;
}

V mul(V a, M b) {
  int n = a.size();
  V res(n, 0);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++)
      res[i] += a[j] * b[j][i] % MOD;
    res[i] %= MOD;
  }
  return res;
}

LL solve(LL C, LL R, LL N) {
  map<LL, vector<LL>> O;
  O[R] = vector<LL>();
  for (LL i = 0, r, c; i < N; i++) {
    cin >> c >> r;
    O[r].push_back(c - 1);
  }
  LL now = 1;
  for (int i = 0; i < C; i++)
    for (int j = 0; j < C; j++)
      d[0][i][j] = 0;
  for (int i = 0; i < C; i++)
    for (int j = max(0, i - 1); j < min((int)C, i + 2); j++)
      d[0][i][j]++;
  pow_init(C);
  V res(C, 0);
  res[0] = 1;
  for (auto &it : O) {
    LL nxt = it.first;
    res = mul(res, mat_pow(C, nxt - now));
    for (auto &c : it.second)
      res[c] = 0;
    now = nxt;
  }
  return res[C - 1];
}
int main() {
  for (LL i = 1, w, h, n; cin >> w >> h >> n, w + h + n; i++) {
    cout << "Case " << i << ": " << solve(w, h, n) << endl;
  }
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

struct I {
  I() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
} init;
typedef long long LL;
const LL MOD = 1e9 + 7;
typedef vector<LL> V;
typedef vector<V> M;

LL d[70][100][100];
void pow_init(int n) {
  for (int b = 1; b < 70; b++) {
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++) {
        d[b][i][j] = 0;
        for (int k = 0; k < n; k++)
          d[b][i][j] += d[b - 1][i][k] * d[b - 1][k][j] % MOD;
        d[b][i][j] %= MOD;
      }
  }
}
M o(int n) { return M(n, V(n, 0)); }
M e(int n) {
  M ret = o(n);
  for (int i = 0; i < n; i++)
    ret[i][i] = 1;
  return ret;
}
M mul(M a, M b) {
  int n = a.size();
  M c = o(n);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < n; k++)
        c[i][j] += a[i][k] * b[k][j] % MOD;
      c[i][j] %= MOD;
    }
  return c;
}
M mul(M a, int m) {
  int n = a.size();
  M c = o(n);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < n; k++)
        c[i][j] += a[i][k] * d[m][k][j] % MOD;
      c[i][j] %= MOD;
    }
  return c;
}
M mat_pow(int m, LL n) {
  M ret = e(m);
  int i = 0;
  while (n) {
    if (n & 1)
      ret = mul(ret, i);
    n >>= 1;
    i++;
  }
  return ret;
}

V mul(V a, M b) {
  int n = a.size();
  V res(n, 0);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++)
      res[i] += a[j] * b[j][i] % MOD;
    res[i] %= MOD;
  }
  return res;
}

LL solve(LL C, LL R, LL N) {
  map<LL, vector<LL>> O;
  O[R] = vector<LL>();
  for (LL i = 0, r, c; i < N; i++) {
    cin >> c >> r;
    O[r].push_back(c - 1);
  }
  int now = 1;
  for (int i = 0; i < C; i++)
    for (int j = 0; j < C; j++)
      d[0][i][j] = 0;
  for (int i = 0; i < C; i++)
    for (int j = max(0, i - 1); j < min((int)C, i + 2); j++)
      d[0][i][j]++;
  pow_init(C);
  V res(C, 0);
  res[0] = 1;
  for (auto &it : O) {
    int nxt = it.first;
    res = mul(res, mat_pow(C, nxt - now));
    for (auto &c : it.second)
      res[c] = 0;
    now = nxt;
  }
  return res[C - 1];
}
int main() {
  for (LL i = 0, w, h, n; cin >> w >> h >> n, w + h + n; i++) {
    cout << "Case " << i << ": " << solve(w, h, n) << endl;
  }
  return 0;
}