Problem p04028 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
#define X first
#define Y second
#define pb emplace_back
#define FOR(i, a, b) for (int(i) = (a); i < (b); ++(i))
#define EFOR(i, a, b) for (int(i) = (a); i <= (b); ++(i))
#define rep(X, Y) for (int(X) = 0; (X) < (Y); ++(X))
#define REP rep
#define rrep(X, Y) for (int(X) = (Y)-1; (X) >= 0; --(X))
#define all(X) (X).begin(), (X).end()
#define eb emplace_back

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef ll LL;
typedef pii PII;
typedef pll PLL;
const ll MOD = 1e9 + 7;

#define rall(X) (X).rbegin(), (X).rend()
#define UNIQUE(X) (X).erase(unique(all(X)), (X).end())
#define reps(X, S, Y) for (int(X) = S; (X) < (Y); ++(X))
#define rreps(X, S, Y) for (int(X) = (Y)-1; (X) >= (S); --(X))
template <class T> inline bool MX(T &l, const T &r) {
  return l < r ? l = r, 1 : 0;
}
template <class T> inline bool MN(T &l, const T &r) {
  return l > r ? l = r, 1 : 0;
}

int N;
string S;
LL dp[5145][11451];

LL mypow(LL base, LL exp) {
  if (!exp)
    return 1;
  LL res = mypow(base * base % MOD, exp / 2);
  if (exp % 2)
    res = res * base % MOD;
  return res;
}

void Add(LL &d, LL v) {
  d += v;
  if (d >= MOD)
    d -= MOD;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(0);
  cin >> N;
  cin >> S;

  dp[0][0] = 1;
  rep(i, N) {
    rep(j, i + 1) {
      if (j > 0)
        Add(dp[i + 1][j - 1], dp[i][j]);
      else
        Add(dp[i + 1][j], dp[i][j]);

      Add(dp[i + 1][j + 1], dp[i][j]);
      Add(dp[i + 1][j + 1], dp[i][j]);
    }
  }

  cout << dp[N][S.size()] * mypow(mypow(2, MOD - 2), S.size()) % MOD << "\n";
}


Fixed submission:
#include <bits/stdc++.h>
#define X first
#define Y second
#define pb emplace_back
#define FOR(i, a, b) for (int(i) = (a); i < (b); ++(i))
#define EFOR(i, a, b) for (int(i) = (a); i <= (b); ++(i))
#define rep(X, Y) for (int(X) = 0; (X) < (Y); ++(X))
#define REP rep
#define rrep(X, Y) for (int(X) = (Y)-1; (X) >= 0; --(X))
#define all(X) (X).begin(), (X).end()
#define eb emplace_back

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef ll LL;
typedef pii PII;
typedef pll PLL;
const ll MOD = 1e9 + 7;

#define rall(X) (X).rbegin(), (X).rend()
#define UNIQUE(X) (X).erase(unique(all(X)), (X).end())
#define reps(X, S, Y) for (int(X) = S; (X) < (Y); ++(X))
#define rreps(X, S, Y) for (int(X) = (Y)-1; (X) >= (S); --(X))
template <class T> inline bool MX(T &l, const T &r) {
  return l < r ? l = r, 1 : 0;
}
template <class T> inline bool MN(T &l, const T &r) {
  return l > r ? l = r, 1 : 0;
}

int N;
string S;
LL dp[5145][5145];

LL mypow(LL base, LL exp) {
  if (!exp)
    return 1;
  LL res = mypow(base * base % MOD, exp / 2);
  if (exp % 2)
    res = res * base % MOD;
  return res;
}

void Add(LL &d, LL v) {
  d += v;
  if (d >= MOD)
    d -= MOD;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(0);
  cin >> N;
  cin >> S;

  dp[0][0] = 1;
  rep(i, N) {
    rep(j, i + 1) {
      if (j > 0)
        Add(dp[i + 1][j - 1], dp[i][j]);
      else
        Add(dp[i + 1][j], dp[i][j]);

      Add(dp[i + 1][j + 1], dp[i][j]);
      Add(dp[i + 1][j + 1], dp[i][j]);
    }
  }

  cout << dp[N][S.size()] * mypow(mypow(2, MOD - 2), S.size()) % MOD << "\n";
}