Problem p02549 (C++)

Buggy submission (status: Runtime Error):
#define LOCAL
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = (n - 1); i >= 0; --i)
#define rep2(i, s, n) for (int i = s; i < (n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define memr(dp, val) memset(dp, val, sizeof(dp))
using namespace std;
typedef long long ll;
static const int INTINF = (INT_MAX >> 1); // 10^9 + 10^7
static const ll LLINF = (LLONG_MAX >> 1);
static const int MAX = 201001;
static const ll MOD = 998244353;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline T int_ceil(T a, T b) {
  T res = a / b;
  if (a % b != 0)
    res++;
  return res;
}
template <typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
string to_string(string s) { return '"' + s + '"'; }
string to_string(bool b) { return b ? "true" : "false"; }
template <size_t N> string to_string(bitset<N> bs) {
  string res;
  for (size_t i = 0; i < N; ++i)
    res += '0' + bs[i];
  return res;
}
string to_string(vector<bool> v) {
  string res = "{";
  for (bool e : v)
    res += to_string(e) + ", ";
  return res += "}";
}
template <class T, class U> string to_string(pair<T, U> p);
template <class C> string to_string(C c) {
  string res = "{";
  for (auto e : c)
    res += to_string(e) + ", ";
  return res += "}";
}
template <class T, class U> string to_string(pair<T, U> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
void debug() { cerr << '\n'; }
template <class Head, class... Tail> void debug(Head head, Tail... tail) {
  cerr << '\t' << to_string(head), debug(tail...);
}
#ifdef LOCAL
#define print(...) cerr << "[" << #__VA_ARGS__ << "]:", debug(__VA_ARGS__)
#else
#define print(...)
#endif

ll dp[MAX];
ll dp_minus[MAX];

int main(int argc, const char *argv[]) {
  std::cout << std::fixed << std::setprecision(15);

  memr(dp, 0);

  dp[0] = 1;
  dp_minus[1] = 1;
  int N, K;
  cin >> N >> K;
  vector<pair<int, int>> lr(K);
  rep(i, K) {
    int l, r;
    cin >> l >> r;
    lr[i] = {l, r};
  }

  rep(i, N) {
    // rep(i, N){
    // 	if(i) cout << " ";
    // 	cout << dp[i];
    // }
    // cout << endl;
    rep(j, K) {
      dp[i + lr[j].first] = (dp[i + lr[j].first] + dp[i]) % MOD;
      dp_minus[i + lr[j].second + 1] =
          (dp_minus[i + lr[j].second + 1] + dp[i]) % MOD;
    }
    dp[i + 1] = (dp[i] + dp[i + 1]) % MOD;
    dp[i + 1] = (dp[i + 1] - dp_minus[i + 1]) % MOD;
    while (dp[i + 1] < 0)
      dp[i + 1] += MOD;
  }
  // rep(i, N){
  // 	if(i) cout << " ";
  // 	cout << dp[i];
  // }
  // cout << endl;

  cout << dp[N - 1] << endl;

  return 0;
}


Fixed submission:
#define LOCAL
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = (n - 1); i >= 0; --i)
#define rep2(i, s, n) for (int i = s; i < (n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define memr(dp, val) memset(dp, val, sizeof(dp))
using namespace std;
typedef long long ll;
static const int INTINF = (INT_MAX >> 1); // 10^9 + 10^7
static const ll LLINF = (LLONG_MAX >> 1);
static const int MAX = 201001;
static const ll MOD = 998244353;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline T int_ceil(T a, T b) {
  T res = a / b;
  if (a % b != 0)
    res++;
  return res;
}
template <typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
string to_string(string s) { return '"' + s + '"'; }
string to_string(bool b) { return b ? "true" : "false"; }
template <size_t N> string to_string(bitset<N> bs) {
  string res;
  for (size_t i = 0; i < N; ++i)
    res += '0' + bs[i];
  return res;
}
string to_string(vector<bool> v) {
  string res = "{";
  for (bool e : v)
    res += to_string(e) + ", ";
  return res += "}";
}
template <class T, class U> string to_string(pair<T, U> p);
template <class C> string to_string(C c) {
  string res = "{";
  for (auto e : c)
    res += to_string(e) + ", ";
  return res += "}";
}
template <class T, class U> string to_string(pair<T, U> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
void debug() { cerr << '\n'; }
template <class Head, class... Tail> void debug(Head head, Tail... tail) {
  cerr << '\t' << to_string(head), debug(tail...);
}
#ifdef LOCAL
#define print(...) cerr << "[" << #__VA_ARGS__ << "]:", debug(__VA_ARGS__)
#else
#define print(...)
#endif

ll dp[MAX];
ll dp_minus[MAX];

int main(int argc, const char *argv[]) {
  std::cout << std::fixed << std::setprecision(15);

  memr(dp, 0);

  dp[0] = 1;
  dp_minus[1] = 1;
  int N, K;
  cin >> N >> K;
  vector<pair<int, int>> lr(K);
  rep(i, K) {
    int l, r;
    cin >> l >> r;
    lr[i] = {l, r};
  }

  rep(i, N) {
    // rep(i, N){
    // 	if(i) cout << " ";
    // 	cout << dp[i];
    // }
    // cout << endl;
    rep(j, K) {
      if (i + lr[j].first >= MAX)
        continue;
      dp[i + lr[j].first] = (dp[i + lr[j].first] + dp[i]) % MOD;
      dp_minus[i + lr[j].second + 1] =
          (dp_minus[i + lr[j].second + 1] + dp[i]) % MOD;
    }
    dp[i + 1] = (dp[i] + dp[i + 1]) % MOD;
    dp[i + 1] = (dp[i + 1] - dp_minus[i + 1]) % MOD;
    while (dp[i + 1] < 0)
      dp[i + 1] += MOD;
  }
  // rep(i, N){
  // 	if(i) cout << " ";
  // 	cout << dp[i];
  // }
  // cout << endl;

  cout << dp[N - 1] << endl;

  return 0;
}