Problem p04028 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
template <class T> ostream &operator<<(ostream &os, vector<T> V) {
  os << "[ ";
  for (auto v : V)
    os << v << " ";
  return os << "]";
}
template <class T> ostream &operator<<(ostream &os, set<T> S) {
  os << "{ ";
  for (auto s : S)
    os << s << " ";
  return os << "}";
}
template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}
template <class L, class R> ostream &operator<<(ostream &os, map<L, R> M) {
  os << "{ ";
  for (auto m : M)
    os << "(" << m.F << ":" << m.S << ")";
  return os << "}";
}
#define cerr cout
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...) 1
#endif
#define ll long long
#define ld long double
#define vll vector<ll>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define I insert
#define pb push_back
#define F first
#define S second
#define endl "\n"
#define mp make_pair
#define all(v) (v).begin(), (v).end()
#define For(i, n) for (int i = 0; i < (int)n; ++i)
#define Rev(i, n) for (int i = (int)n - 1; i >= 0; --i)
#define Rep(i, n) for (int i = 1; i <= (int)n; ++i)
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<pii, int> ppi;
typedef vector<pii> vpii;
#define fio                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)

const int N = 5e3 + 100;
const int mod = 1e9 + 7;
int dp1[N][N]; // dp1[i][j] dentoes number of ways to i moves
               // such that after i moves length is equal to j
int dp2[N][N]; // dp[i][j] dentoes number of ways to have a string of length j
               // after i moves just such that length was always greater than
               // equal to zero
inline int add(int i, int j) {
  i += j;
  if (i >= mod)
    i -= mod;
  return i;
}
inline int mul(int a, int b) { return a * 1ll * b % mod; }
int power(int x, int y) {
  int res = 1;
  while (y) {
    if (y & 1)
      res = mul(res, x);
    x = mul(x, x);
    y >>= 1;
  }
  return res;
}

int pwr[N];
int main() {
  fio;
  cout << fixed << setprecision(25);

  dp1[0][0] = 1;
  dp2[0][0] = 1;
  pwr[0] = 1;
  for (int i = 0; i < N - 1; ++i) {
    pwr[i + 1] = mul(2, pwr[i]);
    if (i != 0)
      assert(dp2[i][0] == 0);
    for (int j = 0; j <= i; ++j) {
      if (j == 0)
        dp1[i + 1][j] = add(dp1[i + 1][j], dp1[i][j]);
      else
        dp1[i + 1][j - 1] = add(dp1[i + 1][j - 1], dp1[i][j]);
      dp1[i + 1][j + 1] = add(dp1[i + 1][j + 1], mul(2, dp1[i][j]));
      dp2[i + 1][j + 1] = add(dp2[i + 1][j + 1], dp2[i][j]);
      if (j >= 2)
        dp2[i + 1][j - 1] = add(dp2[i + 1][j - 1], dp2[i][j]);
    }
  }

  int n;
  string s;
  cin >> n >> s;
  int ans = 0;
  for (int i = 0; i <= n; ++i) {
    if ((n - i - s.size()) % 2 == 0)
      ans = add(ans, mul(dp1[i][0], mul(dp2[n - i][s.size()],
                                        pwr[(n - i - s.size()) / 2])));
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
template <class T> ostream &operator<<(ostream &os, vector<T> V) {
  os << "[ ";
  for (auto v : V)
    os << v << " ";
  return os << "]";
}
template <class T> ostream &operator<<(ostream &os, set<T> S) {
  os << "{ ";
  for (auto s : S)
    os << s << " ";
  return os << "}";
}
template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}
template <class L, class R> ostream &operator<<(ostream &os, map<L, R> M) {
  os << "{ ";
  for (auto m : M)
    os << "(" << m.F << ":" << m.S << ")";
  return os << "}";
}
#define cerr cout
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...) 1
#endif
#define ll long long
#define ld long double
#define vll vector<ll>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define I insert
#define pb push_back
#define F first
#define S second
#define endl "\n"
#define mp make_pair
#define all(v) (v).begin(), (v).end()
#define For(i, n) for (int i = 0; i < (int)n; ++i)
#define Rev(i, n) for (int i = (int)n - 1; i >= 0; --i)
#define Rep(i, n) for (int i = 1; i <= (int)n; ++i)
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<pii, int> ppi;
typedef vector<pii> vpii;
#define fio                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)

const int N = 5e3 + 100;
const int mod = 1e9 + 7;
int dp1[N][N]; // dp1[i][j] dentoes number of ways to i moves
               // such that after i moves length is equal to j
int dp2[N][N]; // dp[i][j] dentoes number of ways to have a string of length j
               // after i moves just such that length was always greater than
               // equal to zero
inline int add(int i, int j) {
  i += j;
  if (i >= mod)
    i -= mod;
  return i;
}
inline int mul(int a, int b) { return a * 1ll * b % mod; }
int power(int x, int y) {
  int res = 1;
  while (y) {
    if (y & 1)
      res = mul(res, x);
    x = mul(x, x);
    y >>= 1;
  }
  return res;
}

int pwr[N];
int main() {
  fio;
  cout << fixed << setprecision(25);

  dp1[0][0] = 1;
  dp2[0][0] = 1;
  pwr[0] = 1;
  for (int i = 0; i < N - 1; ++i) {
    pwr[i + 1] = mul(2, pwr[i]);
    if (i != 0)
      assert(dp2[i][0] == 0);
    for (int j = 0; j <= i; ++j) {
      if (j == 0)
        dp1[i + 1][j] = add(dp1[i + 1][j], dp1[i][j]);
      else
        dp1[i + 1][j - 1] = add(dp1[i + 1][j - 1], dp1[i][j]);
      dp1[i + 1][j + 1] = add(dp1[i + 1][j + 1], mul(2, dp1[i][j]));
      dp2[i + 1][j + 1] = add(dp2[i + 1][j + 1], dp2[i][j]);
      if (j >= 2)
        dp2[i + 1][j - 1] = add(dp2[i + 1][j - 1], dp2[i][j]);
    }
  }

  int n;
  string s;
  cin >> n >> s;
  int ans = 0;
  for (int i = 0; i <= n; ++i) {
    if (i + s.size() > n)
      break;
    if ((n - i - s.size()) % 2 == 0)
      ans = add(ans, mul(dp1[i][0], mul(dp2[n - i][s.size()],
                                        pwr[(n - i - s.size()) / 2])));
  }
  cout << ans << endl;

  return 0;
}