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

/* by Natsu Kinmoe */
#include <bits/stdc++.h>
using namespace std;

#define loop(i, n) for (int i = 0; i < (n); i++)
#define pool(i, n) for (int i = (n)-1; i >= 0; i--)
#define cont(i, n) for (int i = 1; i <= (n); i++)
#define tnoc(i, n) for (int i = (n); i >= 1; i--)
#define circ(i, a, b) for (int i = (a); i <= (b); i++)
#define cric(i, a, b) for (int i = (b); i >= (a); i--)
#define sub(i, a) for (int i = a; i; i = ((i - 1) & (a)))
#define subs(i, a) for (int i = a; i >= 0; i = (!i ? -1 : ((i - 1) & (a))))
#define subt(i, a) for (int i = ((a - 1) & a); i; i = ((i - 1) & (a)))
#define subst(i, a)                                                            \\
  for (int i = ((a - 1) & a); i >= 0; i = (!i ? -1 : ((i - 1) & (a))))
#define range(i, a, b, c)                                                      \\
  for (int i = (a); ((c) > 0 ? i <= (b) : i >= (b)); i += (c))
#define foreach(it, v)                                                         \\
  for (__typeof((v).begin()) it = (v).begin(), _e_D_ = (v).end(); it != _e_D_; \\
       it++)
#define parse(i, s) for (int i = 0; s[i]; i++)

#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define mak make_pair

#define y0 y0O0OO00OO0OO0OO0OOO00OO0OO0O0O000OO0
#define y1 y1II11II11III11I1III11II111IIII1II1I1
#define ws WSrAkIoi2333wSraKIoIWSraKiOi
#define left left_jTjTJtJtIoiiOiioIAuaU66666jtIOIau
#define hash whYusEthiSnamE_caNyoUusEothERnaMesliKeHSH

#define Mask(a) (1ll << (a))
#define Maskx(a, x) ((a) << (x))
#define Full(a) ((1ll << (a)) - 1)
#define Bit(a, x) (((a) >> (x)) & 1)
#define all(a) (a).begin(), (a).end()
#define SZ(a) ((int)((a).size()))

typedef long long ll;
typedef long double lf;

const int Inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const lf inF = 11451419198101145141919810.1145141919810;
const lf pi = acosl(-1);
/* Source code starts here */

int n, a, b;
const int jt = 1000000007;
int dp[5005][5005][2], qzh1[5005], qzh2[5005][5005];
void inline add(int &a, int b) {
  a += b;
  if (a >= jt)
    a -= jt;
  if (a < 0)
    a += jt;
}

int main() {
  scanf("%d%d%d", &n, &a, &b);
  if (a < b)
    swap(a, b);
  dp[0][0][1] = 1;
  loop(i, n + 1) {
    if (i)
      add(qzh1[i], qzh1[i - 1]);
    add(dp[i][0][0], qzh1[i]);
    loop(j, i + 1) {
      if (i)
        add(qzh2[i - j][i], qzh2[i - j][i - 1]);
      add(dp[i][j][0], qzh2[i - j][i]);
      if (j == a) {
        add(dp[i][j][1], dp[i][j][0]);
        dp[i][j][0] = 0;
        add(dp[i + 1][j][1], dp[i][j][1]);
        add(dp[i + 1][j][1], dp[i][j][1]);
        break;
      }
      add(dp[i + 1][j + 1][1], dp[i][j][0]);
      add(dp[i + 1][j + 1][1], dp[i][j][1]);
      add(qzh1[i + 1], dp[i][j][1]);
      if (i + b <= n) {
        add(qzh1[i + b], -dp[i][j][1]);
        if (j + b <= a)
          add(qzh2[i - j][i + b], dp[i][j][1]);
        else
          add(dp[i + b][a][1], dp[i][j][1]);
      }
    }
  }
  add(dp[n][a][1], dp[n][a][0]);
  printf("%d\\n", dp[n][a][1]);
  return 0;
}

Error: -11

Fixed submission:

/* by Natsu Kinmoe */
#include <bits/stdc++.h>
using namespace std;

#define loop(i, n) for (int i = 0; i < (n); i++)
#define pool(i, n) for (int i = (n)-1; i >= 0; i--)
#define cont(i, n) for (int i = 1; i <= (n); i++)
#define tnoc(i, n) for (int i = (n); i >= 1; i--)
#define circ(i, a, b) for (int i = (a); i <= (b); i++)
#define cric(i, a, b) for (int i = (b); i >= (a); i--)
#define sub(i, a) for (int i = a; i; i = ((i - 1) & (a)))
#define subs(i, a) for (int i = a; i >= 0; i = (!i ? -1 : ((i - 1) & (a))))
#define subt(i, a) for (int i = ((a - 1) & a); i; i = ((i - 1) & (a)))
#define subst(i, a)                                                            \\
  for (int i = ((a - 1) & a); i >= 0; i = (!i ? -1 : ((i - 1) & (a))))
#define range(i, a, b, c)                                                      \\
  for (int i = (a); ((c) > 0 ? i <= (b) : i >= (b)); i += (c))
#define foreach(it, v)                                                         \\
  for (__typeof((v).begin()) it = (v).begin(), _e_D_ = (v).end(); it != _e_D_; \\
       it++)
#define parse(i, s) for (int i = 0; s[i]; i++)

#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define mak make_pair

#define y0 y0O0OO00OO0OO0OO0OOO00OO0OO0O0O000OO0
#define y1 y1II11II11III11I1III11II111IIII1II1I1
#define ws WSrAkIoi2333wSraKIoIWSraKiOi
#define left left_jTjTJtJtIoiiOiioIAuaU66666jtIOIau
#define hash whYusEthiSnamE_caNyoUusEothERnaMesliKeHSH

#define Mask(a) (1ll << (a))
#define Maskx(a, x) ((a) << (x))
#define Full(a) ((1ll << (a)) - 1)
#define Bit(a, x) (((a) >> (x)) & 1)
#define all(a) (a).begin(), (a).end()
#define SZ(a) ((int)((a).size()))

typedef long long ll;
typedef long double lf;

const int Inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const lf inF = 11451419198101145141919810.1145141919810;
const lf pi = acosl(-1);
/* Source code starts here */

int n, a, b;
const int jt = 1000000007;
int dp[5005][5005][2], qzh1[5005], qzh2[5005][5005];
void inline add(int &a, int b) {
  a += b;
  if (a >= jt)
    a -= jt;
  if (a < 0)
    a += jt;
}

int main() {
  scanf("%d%d%d", &n, &a, &b);
  if (a < b)
    swap(a, b);
  dp[0][0][1] = 1;
  loop(i, n + 1) {
    if (i)
      add(qzh1[i], qzh1[i - 1]);
    add(dp[i][0][0], qzh1[i]);
    loop(j, i + 1) {
      if (i)
        add(qzh2[i - j][i], qzh2[i - j][i - 1]);
      add(dp[i][j][0], qzh2[i - j][i]);
      if (j == a) {
        add(dp[i][j][1], dp[i][j][0]);
        dp[i][j][0] = 0;
        add(dp[i + 1][j][1], dp[i][j][1]);
        add(dp[i + 1][j][1], dp[i][j][1]);
        break;
      }
      add(dp[i + 1][j + 1][1], dp[i][j][0]);
      add(dp[i + 1][j + 1][1], dp[i][j][1]);
      add(qzh1[i + 1], dp[i][j][1]);
      add(qzh1[i + b], -dp[i][j][1]);
      if (j + b <= a)
        add(qzh2[i - j][i + b], dp[i][j][1]);
      else
        add(dp[i + b][a][1], dp[i][j][1]);
    }
  }
  add(dp[n][a][1], dp[n][a][0]);
  printf("%d\\n", dp[n][a][1]);
  return 0;
}