Problem p03222 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rep2(i, a, b) for (ll i = a; i < b; ++i)
const ll MOD = 1000000007;
string alp = "abcdefghijklmnopqrstuvwxyz";

int main() {
  int H, W, K;
  cin >> H >> W >> K;
  vector<vector<vector<ll>>> pos1(W, vector<vector<ll>>(W, vector<ll>(W, 0)));
  vector<vector<vector<ll>>> pos2(W, vector<vector<ll>>(W, vector<ll>(W, 0)));
  rep(i, W) {
    rep(j, W) {
      pos2[i][j][0] = 1;
      if (i == 0 && j == 0) {
        pos1[i][j][0] = 1;
        pos2[i][j][0] = 0;
      }
      rep(k, W - 1) {
        if (k + 1 == i || k + 1 == j) {
          if (i == j) {
            pos1[i][j][k + 1] = 0;
            pos2[i][j][k + 1] = (pos1[i][j][k] + pos2[i][j][k]) % MOD;
            if (k + 2 <= W - 1) {
              pos1[i][j][k + 2] = 0;
              pos2[i][j][k + 2] = pos2[i][j][k + 1];
              k++;
            }
          } else if ((i == 0 && j == 1) || (i == 1 && j == 0)) {
            pos1[i][j][k + 1] = 1;
            pos2[i][j][k + 1] = 0;
          } else {
            pos1[i][j][k + 1] = 0;
            pos2[i][j][k + 1] = (pos1[i][j][k] + pos2[i][j][k]) % MOD;
            pos1[i][j][k + 2] = pos2[i][j][k + 1];
            pos2[i][j][k + 2] = 0;
            k++;
          }
        } else {
          pos1[i][j][k + 1] = pos2[i][j][k];
          pos2[i][j][k + 1] = (pos1[i][j][k] + pos2[i][j][k]) % MOD;
        }
      }
    }
  }
  vector<vector<ll>> dp(H + 1, vector<ll>(8, 0));
  dp[0][0] = 1;
  rep(i, H) {
    rep(j, W) {
      dp[i + 1][j] += dp[i][j] * (pos1[j][j][W - 1] + pos2[j][j][W - 1]);
      if (j > 0) {
        dp[i + 1][j] +=
            dp[i][j - 1] * (pos1[j - 1][j][W - 1] + pos2[j - 1][j][W - 1]);
      }
      if (j < W - 1) {
        dp[i + 1][j] +=
            dp[i][j + 1] * (pos1[j + 1][j][W - 1] + pos2[j + 1][j][W - 1]);
      }
      dp[i + 1][j] %= MOD;
    }
  }
  cout << dp[H][K - 1] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rep2(i, a, b) for (ll i = a; i < b; ++i)
const ll MOD = 1000000007;
string alp = "abcdefghijklmnopqrstuvwxyz";

int main() {
  int H, W, K;
  cin >> H >> W >> K;
  vector<vector<vector<ll>>> pos1(W, vector<vector<ll>>(W, vector<ll>(W, 0)));
  vector<vector<vector<ll>>> pos2(W, vector<vector<ll>>(W, vector<ll>(W, 0)));
  rep(i, W) {
    rep(j, W) {
      pos2[i][j][0] = 1;
      if (i == 0 && j == 0) {
        pos1[i][j][0] = 1;
        pos2[i][j][0] = 0;
      }
      rep(k, W - 1) {
        if (k + 1 == i || k + 1 == j) {
          if (i == j) {
            pos1[i][j][k + 1] = 0;
            pos2[i][j][k + 1] = (pos1[i][j][k] + pos2[i][j][k]) % MOD;
            if (k + 2 <= W - 1) {
              pos1[i][j][k + 2] = 0;
              pos2[i][j][k + 2] = pos2[i][j][k + 1];
              k++;
            }
          } else if ((i == 0 && j == 1) || (i == 1 && j == 0)) {
            pos1[i][j][k + 1] = 1;
            pos2[i][j][k + 1] = 0;
          } else {
            pos1[i][j][k + 1] = 0;
            pos2[i][j][k + 1] = (pos1[i][j][k] + pos2[i][j][k]) % MOD;
            if (k + 2 <= W - 1) {
              pos1[i][j][k + 2] = pos2[i][j][k + 1];
              pos2[i][j][k + 2] = 0;
              k++;
            }
          }
        } else {
          pos1[i][j][k + 1] = pos2[i][j][k];
          pos2[i][j][k + 1] = (pos1[i][j][k] + pos2[i][j][k]) % MOD;
        }
      }
    }
  }
  vector<vector<ll>> dp(H + 1, vector<ll>(8, 0));
  dp[0][0] = 1;
  rep(i, H) {
    rep(j, W) {
      dp[i + 1][j] += dp[i][j] * (pos1[j][j][W - 1] + pos2[j][j][W - 1]);
      if (j > 0) {
        dp[i + 1][j] +=
            dp[i][j - 1] * (pos1[j - 1][j][W - 1] + pos2[j - 1][j][W - 1]);
      }
      if (j < W - 1) {
        dp[i + 1][j] +=
            dp[i][j + 1] * (pos1[j + 1][j][W - 1] + pos2[j + 1][j][W - 1]);
      }
      dp[i + 1][j] %= MOD;
    }
  }
  cout << dp[H][K - 1] << endl;
}