Problem p02549 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"

#define MOD 998244353
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)
#define ALL(v) v.begin(), v.end()
#define rALL(v) v.rbegin(), v.rend()
#define FOR(i, j, k) for (ll i = j; i < k; i++)
#define debug_print(var) cerr << #var << "=" << var << endl;
#define DUMP(i, v)                                                             \
  for (ll i = 0; i < v.size(); i++)                                            \
  cerr << v[i] << " "
#define fi first
#define se second

using namespace std;
typedef long long int ll;
typedef vector<ll> llvec;
typedef vector<double> dvec;
typedef pair<ll, ll> P;
typedef long double ld;
struct edge {
  ll x, c;
};

ll mod(ll a, ll mod) {
  ll res = a % mod;
  if (res < 0)
    res = res + mod;
  return res;
}

ll modpow(ll a, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

ll modinv(ll a, ll mod) {
  ll b = mod, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= mod;
  if (u < 0)
    u += mod;
  return u;
}

ll gcd(ll a, ll b) {
  ll r = a % b;
  if (r == 0)
    return b;
  else
    return gcd(b, a % b);
}

bool is_prime(ll n) {
  ll i = 2;
  if (n == 1)
    return false;
  if (n == 2)
    return true;
  bool res = true;
  while (i * i < n) {
    if (n % i == 0) {
      res = false;
    }
    i = i + 1;
  }

  // if(i==1)res = false;
  if (n % i == 0)
    res = false;
  return res;
}
/**************************************
** A main function starts from here  **
***************************************/
int main() {
  ll N, K;
  cin >> N >> K;
  llvec L(K), R(K);
  rep(i, K) { cin >> L[i] >> R[i]; }
  llvec dp(N, 0);
  llvec a(N, 0);
  dp[0] = 1;
  rep(i, N) {
    if (i > 0)
      a[i] = mod(a[i] + a[i - 1], MOD);
    dp[i] = mod(dp[i] + a[i], MOD);
    rep(j, K) {
      if (L[j] + 1 < N)
        a[i + L[j]] = mod(a[i + L[j]] + dp[i], MOD);
      if (i + R[j] + 1 < N)
        a[i + R[j] + 1] = mod(a[i + R[j] + 1] - dp[i], MOD);
    }
  }
  cout << dp[N - 1];
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"

#define MOD 998244353
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)
#define ALL(v) v.begin(), v.end()
#define rALL(v) v.rbegin(), v.rend()
#define FOR(i, j, k) for (ll i = j; i < k; i++)
#define debug_print(var) cerr << #var << "=" << var << endl;
#define DUMP(i, v)                                                             \
  for (ll i = 0; i < v.size(); i++)                                            \
  cerr << v[i] << " "
#define fi first
#define se second

using namespace std;
typedef long long int ll;
typedef vector<ll> llvec;
typedef vector<double> dvec;
typedef pair<ll, ll> P;
typedef long double ld;
struct edge {
  ll x, c;
};

ll mod(ll a, ll mod) {
  ll res = a % mod;
  if (res < 0)
    res = res + mod;
  return res;
}

ll modpow(ll a, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

ll modinv(ll a, ll mod) {
  ll b = mod, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= mod;
  if (u < 0)
    u += mod;
  return u;
}

ll gcd(ll a, ll b) {
  ll r = a % b;
  if (r == 0)
    return b;
  else
    return gcd(b, a % b);
}

bool is_prime(ll n) {
  ll i = 2;
  if (n == 1)
    return false;
  if (n == 2)
    return true;
  bool res = true;
  while (i * i < n) {
    if (n % i == 0) {
      res = false;
    }
    i = i + 1;
  }

  // if(i==1)res = false;
  if (n % i == 0)
    res = false;
  return res;
}
/**************************************
** A main function starts from here  **
***************************************/
int main() {
  ll N, K;
  cin >> N >> K;
  llvec L(K), R(K);
  rep(i, K) { cin >> L[i] >> R[i]; }
  llvec dp(N, 0);
  llvec a(N, 0);
  dp[0] = 1;
  rep(i, N) {
    if (i > 0)
      a[i] = mod(a[i] + a[i - 1], MOD);
    dp[i] = mod(dp[i] + a[i], MOD);
    rep(j, K) {
      if (L[j] + i < N)
        a[i + L[j]] = mod(a[i + L[j]] + dp[i], MOD);
      if (i + R[j] + 1 < N)
        a[i + R[j] + 1] = mod(a[i + R[j] + 1] - dp[i], MOD);
    }
  }
  cout << dp[N - 1];
  return 0;
}