Problem p03222 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
const int INF = 1 << 28;
const ll MOD = 1'000'000'007;
template <class T> bool chmax(T &a, const T &b) {
  return (a < b) ? (a = b, 1) : 0;
}
template <class T> bool chmin(T &a, const T &b) {
  return (b < a) ? (a = b, 1) : 0;
}

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  vvl dp(h + 1, vl(w, 0));
  dp[0][0] = 1;
  vl cases(7, 0);
  cases[0] = 1;
  for (int i = 1; i <= 7; ++i) {
    for (int j = 0; j < 1 << i; ++j) {
      bool valid = true;
      for (int k = 0; k < i - 1; ++k) {
        if ((j & 1 << k) && (j & 1 << (k + 1))) {
          valid = false;
          break;
        }
      }
      if (valid)
        cases[i]++;
    }
  }
  for (int i = 1; i <= h; ++i) {
    for (int j = 0; j < w; ++j) {
      ll tempmid = dp[i - 1][j];
      if (j > 1)
        tempmid = tempmid * cases[j - 1] % MOD;
      if (j < w - 2)
        tempmid = tempmid * cases[w - (j + 2)] % MOD;
      ll templeft = 0;
      ll tempright = 0;
      if (j > 0) {
        templeft = dp[i - 1][j - 1];
        if (j > 2)
          templeft = templeft * cases[j - 2] % MOD;
        if (j < w - 2)
          templeft = templeft * cases[w - (j + 2)] % MOD;
      }
      if (j < w - 1) {
        tempright = dp[i - 1][j + 1];
        if (j > 1)
          tempright = tempright * cases[j - 1] % MOD;
        if (j < w - 3)
          tempright = tempright * cases[w - (j + 3)] % MOD;
      }
      dp[i][j] = (tempmid + templeft + tempright) % MOD;
    }
  }
  cout << dp[h][k - 1] << endl;
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
const int INF = 1 << 28;
const ll MOD = 1'000'000'007;
template <class T> bool chmax(T &a, const T &b) {
  return (a < b) ? (a = b, 1) : 0;
}
template <class T> bool chmin(T &a, const T &b) {
  return (b < a) ? (a = b, 1) : 0;
}

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  vvl dp(h + 1, vl(w, 0));
  dp[0][0] = 1;
  vl cases(8, 0);
  cases[0] = 1;
  for (int i = 1; i <= 7; ++i) {
    for (int j = 0; j < 1 << i; ++j) {
      bool valid = true;
      for (int k = 0; k < i - 1; ++k) {
        if ((j & 1 << k) && (j & 1 << (k + 1))) {
          valid = false;
          break;
        }
      }
      if (valid)
        cases[i]++;
    }
  }
  for (int i = 1; i <= h; ++i) {
    for (int j = 0; j < w; ++j) {
      ll tempmid = dp[i - 1][j];
      if (j > 1)
        tempmid = tempmid * cases[j - 1] % MOD;
      if (j < w - 2)
        tempmid = tempmid * cases[w - (j + 2)] % MOD;
      ll templeft = 0;
      ll tempright = 0;
      if (j > 0) {
        templeft = dp[i - 1][j - 1];
        if (j > 2)
          templeft = templeft * cases[j - 2] % MOD;
        if (j < w - 2)
          templeft = templeft * cases[w - (j + 2)] % MOD;
      }
      if (j < w - 1) {
        tempright = dp[i - 1][j + 1];
        if (j > 1)
          tempright = tempright * cases[j - 1] % MOD;
        if (j < w - 3)
          tempright = tempright * cases[w - (j + 3)] % MOD;
      }
      dp[i][j] = (tempmid + templeft + tempright) % MOD;
    }
  }
  cout << dp[h][k - 1] << endl;
  return 0;
}