Problem p02653 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ii pair<int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock() / CLOCKS_PER_SEC

const int N = 5007, MOD = 1000 * 1000 * 1000 + 7;

int mod(int n) {
  n %= MOD;
  if (n < 0)
    return n + MOD;
  else
    return n;
}
int fp(int a, int p) {
  int ans = 1, c = a;
  for (int i = 0; (1ll << i) <= p; ++i) {
    if ((p >> i) & 1)
      ans = mod(ans * c);
    c = mod(c * c);
  }
  return ans;
}
int dv(int a, int b) { return mod(a * fp(b, MOD - 2)); }
void add(int &a, int b) { a = mod(a + b); }

int dp[N][N][2], t1[N][N][2], t2[N][N][2];
signed main() {
#ifdef HOME
  freopen("input.txt", "r", stdin);
#else
#define endl '\\n'
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#endif

  int n, a, b;
  cin >> n >> a >> b;

  if (b < a)
    swap(a, b);

  for (int i = 1; i <= n; ++i) {
    for (int t = 0; t < 2; ++t) {
      int to = i;
      if (t == 0 && i < a)
        to = 0;
      dp[i][to][t] = 1;
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j < b; ++j) {
      for (int t = 0; t < 2; ++t) {

        add(t1[i][j][t], t1[i - 1][j][t]);
        add(dp[i][j][t], t1[i][j][t]);
        if (i && j) {
          add(t2[i][j][t], t2[i - 1][j - 1][t]);
        }
        add(dp[i][j][t], t2[i][j][t]);
        int x = dp[i][j][t];

        if (t) {
          if (1 < a) {
            add(t1[i + 1][0][t ^ 1], x);
            if (i + a <= n) {
              add(t1[i + a][0][t ^ 1], -x);
            }
          }
          if (a < b - j) {
            if (i + a <= n && j + a <= n) {
              add(t2[i + a][j + a][t ^ 1], x);
            }
            if (i + b - j <= n && j + b - j <= n) {
              add(t2[i + b - j][j + b - j][t ^ 1], -x);
            }
          }
        } else {
          if (1 < b - j) {
            add(t2[i + 1][j + 1][t ^ 1], x);
            if (i + b - j <= n && j + b - j <= n) {
              add(t2[i + b - j][j + b - j][t ^ 1], -x);
            }
          }
          /*
          for (int k = 1; k < b - j; ++k)
              add(dp[i + k][j + k][t^1], x);
          */
        }
      }
    }
  }

  int ans = 1;
  for (int i = 0; i < n; ++i)
    ans = (ans << 1) % MOD;
  for (int i = 0; i < b; ++i)
    for (int t = 0; t < 2; ++t)
      ans = mod(ans - dp[n][i][t]);
  cout << ans << endl;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ii pair<int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock() / CLOCKS_PER_SEC

const int N = 5007, MOD = 1000 * 1000 * 1000 + 7;

int mod(int n) {
  n %= MOD;
  if (n < 0)
    return n + MOD;
  else
    return n;
}
int fp(int a, int p) {
  int ans = 1, c = a;
  for (int i = 0; (1ll << i) <= p; ++i) {
    if ((p >> i) & 1)
      ans = mod(ans * c);
    c = mod(c * c);
  }
  return ans;
}
int dv(int a, int b) { return mod(a * fp(b, MOD - 2)); }
void add(int &a, int b) { a = mod(a + b); }

int dp[N][N][2], t1[N][N][2], t2[N][N][2];
signed main() {
#ifdef HOME
  freopen("input.txt", "r", stdin);
#else
#define endl '\\n'
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#endif

  int n, a, b;
  cin >> n >> a >> b;

  if (b < a)
    swap(a, b);

  for (int i = 1; i <= n; ++i) {
    for (int t = 0; t < 2; ++t) {
      int to = i;
      if (t == 0 && i < a)
        to = 0;
      dp[i][to][t] = 1;
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j < b; ++j) {
      for (int t = 0; t < 2; ++t) {

        add(t1[i][j][t], t1[i - 1][j][t]);
        add(dp[i][j][t], t1[i][j][t]);
        if (i && j) {
          add(t2[i][j][t], t2[i - 1][j - 1][t]);
        }
        add(dp[i][j][t], t2[i][j][t]);
        int x = dp[i][j][t];

        if (t) {
          if (1 < a) {
            add(t1[i + 1][0][t ^ 1], x);
            if (i + a <= n) {
              add(t1[i + a][0][t ^ 1], -x);
            }
          }
          if (a < b - j) {
            if (i + a <= n && j + a <= n) {
              add(t2[i + a][j + a][t ^ 1], x);
            }
            if (i + b - j <= n && j + b - j <= n) {
              add(t2[i + b - j][j + b - j][t ^ 1], -x);
            }
          }
          /*
          for (int k = a; k < b - j; ++k)
              add(dp[i + k][j + k][t^1], x);
          */
        } else {
          for (int k = 1; k < b - j; ++k)
            add(dp[i + k][j + k][t ^ 1], x);
        }
      }
    }
  }

  int ans = 1;
  for (int i = 0; i < n; ++i)
    ans = (ans << 1) % MOD;
  for (int i = 0; i < b; ++i)
    for (int t = 0; t < 2; ++t)
      ans = mod(ans - dp[n][i][t]);
  cout << ans << endl;
}