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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;

#define EACH(i, a) for (auto &i : a)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define RFOR(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define RREP(i, n) for (ll i = (n)-1; i >= 0; i--)
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define ALL(a) (a).begin(), (a).end()

const ll linf = 1e18;
const int inf = 1e9;
const double eps = 1e-12;
const double pi = acos(-1);

template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  EACH(x, vec) is >> x;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
  REP(i, vec.size()) {
    if (i)
      os << " ";
    os << vec[i];
  }
  return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) {
  REP(i, vec.size()) {
    if (i)
      os << endl;
    os << vec[i];
  }
  return os;
}

typedef vector<ll> Row;
typedef vector<Row> Matrix;

const ll mod = 1e9 + 9;

ll add(ll a, ll b) { return (a + b) % mod; }
ll mul(ll a, ll b) { return a * b % mod; }
Matrix mul(Matrix A, Matrix B) {
  Matrix res(A.size(), Row(B[0].size(), 0));
  REP(i, res.size()) REP(j, res[i].size()) REP(k, B.size()) {
    res[i][j] = add(res[i][j], mul(A[i][k], B[k][j]));
  }
  return res;
}
Row mul(Matrix A, Row B) {
  Row res(B.size(), 0);
  REP(i, res.size()) REP(j, A[i].size()) {
    res[i] = add(res[i], mul(A[i][j], B[j]));
  }
  return res;
}
Matrix power(Matrix A, ll n) {
  Matrix res(A.size(), Row(A.size(), 0));
  REP(i, res.size()) res[i][i] = 1;
  for (ll i = 1; i <= n; i <<= 1) {
    if (i & n)
      res = mul(res, A);
    A = mul(A, A);
  }
  return res;
}

Row dp(const Row &prev, const Row &m) {
  int W = prev.size();
  Row res(W, 0);
  REP(i, W) {
    if (m[i] == 0) {
      if (i > 0)
        res[i] = add(res[i], prev[i - 1]);
      res[i] = add(res[i], prev[i]);
      if (i < W - 1)
        res[i] = add(res[i], prev[i + 1]);
    }
  }
  return res;
}
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  ll W, H, N;
  int id = 1;
  while (cin >> W >> H >> N, W || H || N) {
    map<ll, Row> m;
    REP(i, N) {
      ll x, y;
      cin >> x >> y;
      --y, --x;
      if (y == 0)
        continue;
      if (m.count(y) == 0)
        m[y].assign(W, 0);
      m[y][x] = 1;
    }
    Matrix A(W, Row(W, 0));
    REP(i, W) {
      REP(j, W) {
        if (abs(i - j) <= 1)
          A[i][j] = 1;
      }
    }
    ll pos = 0;
    Row v(W, 0);
    v[0] = 1;
    EACH(p, m) {
      ll ny = p.first;
      if (pos < ny - 1) {
        v = mul(power(A, ny - 1 - pos), v);
      }
      v = dp(v, p.second);
      pos = ny;
    }
    v = mul(power(A, H - 1 - pos), v);
    cout << "Case " << id++ << ": " << v[W - 1] << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;

#define EACH(i, a) for (auto &i : a)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define RFOR(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define RREP(i, n) for (ll i = (n)-1; i >= 0; i--)
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define ALL(a) (a).begin(), (a).end()

const ll linf = 1e18;
const int inf = 1e9;
const double eps = 1e-12;
const double pi = acos(-1);

template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  EACH(x, vec) is >> x;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
  REP(i, vec.size()) {
    if (i)
      os << " ";
    os << vec[i];
  }
  return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) {
  REP(i, vec.size()) {
    if (i)
      os << endl;
    os << vec[i];
  }
  return os;
}

typedef vector<ll> Row;
typedef vector<Row> Matrix;

const ll mod = 1e9 + 9;

ll add(ll a, ll b) { return (a + b) % mod; }
ll mul(ll a, ll b) { return a * b % mod; }
Matrix mul(Matrix A, Matrix B) {
  Matrix res(A.size(), Row(B[0].size(), 0));
  REP(i, res.size()) REP(j, res[i].size()) REP(k, B.size()) {
    res[i][j] = add(res[i][j], mul(A[i][k], B[k][j]));
  }
  return res;
}
Row mul(Matrix A, Row B) {
  Row res(B.size(), 0);
  REP(i, res.size()) REP(j, A[i].size()) {
    res[i] = add(res[i], mul(A[i][j], B[j]));
  }
  return res;
}
Matrix power(Matrix A, ll n) {
  Matrix res(A.size(), Row(A.size(), 0));
  REP(i, res.size()) res[i][i] = 1;
  for (ll i = 1; i <= n; i <<= 1) {
    if (i & n)
      res = mul(res, A);
    A = mul(A, A);
  }
  return res;
}

Row dp(const Row &prev, const Row &m) {
  int W = prev.size();
  Row res(W, 0);
  REP(i, W) {
    if (m[i] == 0) {
      if (i > 0)
        res[i] = add(res[i], prev[i - 1]);
      res[i] = add(res[i], prev[i]);
      if (i < W - 1)
        res[i] = add(res[i], prev[i + 1]);
    }
  }
  return res;
}
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  int W, H, N;
  int id = 1;
  while (cin >> W >> H >> N, W || H || N) {
    map<ll, Row> m;
    REP(i, N) {
      ll x, y;
      cin >> x >> y;
      --y, --x;
      if (y == 0)
        continue;
      if (m.count(y) == 0)
        m[y].assign(W, 0);
      m[y][x] = 1;
    }
    Matrix A(W, Row(W, 0));
    REP(i, W) {
      REP(j, H) {
        if (abs(i - j) <= 1)
          A[i][j] = 1;
      }
    }
    ll pos = 0;
    Row v(W, 0);
    v[0] = 1;
    EACH(p, m) {
      ll ny = p.first;
      if (pos < ny - 1) {
        v = mul(power(A, ny - 1 - pos), v);
      }
      v = dp(v, p.second);
      pos = ny;
    }
    v = mul(power(A, H - 1 - pos), v);
    cout << "Case " << id++ << ": " << v[W - 1] << endl;
  }
}