Problem p03222 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define pt(sth) cout << sth << "\n"
#define chmax(a, b)                                                            \
  {                                                                            \
    if (a < b)                                                                 \
      a = b;                                                                   \
  }
#define chmin(a, b)                                                            \
  {                                                                            \
    if (a > b)                                                                 \
      a = b;                                                                   \
  }
#define moC(a, s, b) (a) = ((a)s(b) + MOD) % MOD
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
static const ll INF = 1e18;
static const ll MAX = 101010;
static const ll MOD = 1e9 + 7;

ll moP(ll x, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      moC(res, *, x);
    moC(x, *, x);
    n >>= 1;
  }
  return res;
}

ll H, W, K;

ll f(ll w) {
  ll i;
  ll res = 0;

  for (ll S = 0; S < 1 << w; S++) {
    vector<ll> v;
    for (i = 0; i < w; i++) {
      if (S >> i & 1)
        v.push_back(i);
    }

    ll f = 1;
    for (i = 0; i < (ll)v.size() - 1; i++) {
      if (v[i] + 1 == v[i + 1])
        f = 0;
    }

    if (f)
      res++;
  }

  return res;
}

int main(void) {
  cin >> H >> W >> K;
  ll i, j, k, l;
  ll dp[111][11] = {};

  dp[0][1] = 1;
  for (i = 0; i < H; i++) {
    for (j = 1; j <= W; j++) {

      // left
      if (j == 2 || j == W) {
        moC(dp[i + 1][j - 1], +, dp[i][j] * f(W - 3) % MOD);
      } else if (2 < j && j < W) {
        moC(dp[i + 1][j - 1], +,
            dp[i][j] * f(j - 3) % MOD * f(W - j - 1) % MOD);
      }

      // right
      if (j == 1 || j == W - 1) {
        moC(dp[i + 1][j + 1], +, dp[i][j] * f(W - 3) % MOD);
      } else if (1 < j && j < W - 1) {
        moC(dp[i + 1][j + 1], +,
            dp[i][j] * f(j - 2) % MOD * f(W - j - 2) % MOD);
      }

      // down
      if (j == 1 || j == W) {
        moC(dp[i + 1][j], +, dp[i][j] * f(W - 2) % MOD);
      } else {
        moC(dp[i + 1][j], +, dp[i][j] * f(j - 2) % MOD * f(W - j - 1) % MOD);
      }
    }
  }

  pt(dp[H][K]);
}


Fixed submission:
#include <bits/stdc++.h>
#define pt(sth) cout << sth << "\n"
#define chmax(a, b)                                                            \
  {                                                                            \
    if (a < b)                                                                 \
      a = b;                                                                   \
  }
#define chmin(a, b)                                                            \
  {                                                                            \
    if (a > b)                                                                 \
      a = b;                                                                   \
  }
#define moC(a, s, b) (a) = ((a)s(b) + MOD) % MOD
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
static const ll INF = 1e18;
static const ll MAX = 101010;
static const ll MOD = 1e9 + 7;

ll moP(ll x, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      moC(res, *, x);
    moC(x, *, x);
    n >>= 1;
  }
  return res;
}

ll H, W, K;

ll f(ll w) {
  if (w < 0)
    return 1;
  ll i;
  ll res = 0;

  for (ll S = 0; S < 1 << w; S++) {
    vector<ll> v;
    for (i = 0; i < w; i++) {
      if (S >> i & 1)
        v.push_back(i);
    }

    ll f = 1;
    for (i = 0; i < (ll)v.size() - 1; i++) {
      if (v[i] + 1 == v[i + 1])
        f = 0;
    }

    if (f)
      res++;
  }

  return res;
}

int main(void) {
  cin >> H >> W >> K;
  ll i, j, k, l;
  ll dp[111][11] = {};

  dp[0][1] = 1;
  for (i = 0; i < H; i++) {
    for (j = 1; j <= W; j++) {

      // left
      if (j == 2 || j == W) {
        moC(dp[i + 1][j - 1], +, dp[i][j] * f(W - 3) % MOD);
      } else if (2 < j && j < W) {
        moC(dp[i + 1][j - 1], +,
            dp[i][j] * f(j - 3) % MOD * f(W - j - 1) % MOD);
      }

      // right
      if (j == 1 || j == W - 1) {
        moC(dp[i + 1][j + 1], +, dp[i][j] * f(W - 3) % MOD);
      } else if (1 < j && j < W - 1) {
        moC(dp[i + 1][j + 1], +,
            dp[i][j] * f(j - 2) % MOD * f(W - j - 2) % MOD);
      }

      // down
      if (j == 1 || j == W) {
        moC(dp[i + 1][j], +, dp[i][j] * f(W - 2) % MOD);
      } else {
        moC(dp[i + 1][j], +, dp[i][j] * f(j - 2) % MOD * f(W - j - 1) % MOD);
      }
    }
  }

  pt(dp[H][K]);
}