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

#include <bits/stdc++.h>
#define ll long long
#define MODV 998244353
#define INFLL (1LL << 62) // 9223372036854775807
#define EPS 1e-9
#define rep(i, n) for (ll i = 0, i##_len = (ll)(n); i < i##_len; i++)
#define repf(i, n) for (ll i = 1, i##_len = (ll)(n + 1); i < i##_len; i++)
#define all(v) v.begin(), v.end()
#define endl "\\n"
#define vi vector<ll>
#define vvi vector<vector<ll>>
#define Yes() cout << "Yes" << endl
#define YES() cout << "YES" << endl
#define No() cout << "No" << endl
#define NO() cout << "NO" << endl
#define Init()                                                                 \\
  std::ios::sync_with_stdio(false);                                            \\
  std::cin.tie(0);                                                             \\
  std::cout << fixed << setprecision(15);
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;

int main() {
  Init();
  ll n, k;
  cin >> n >> k;
  vector<pair<ll, ll>> lr(k);
  rep(i, k) cin >> lr[i].first >> lr[i].second;
  sort(all(lr));

  vi dp(n * 2 + 1, 0), sect(n * 2 + 1, 0);
  dp[1] = 1, sect[1] = 1;

  for (ll i = 1; i <= n; i++) {
    dp[i] = dp[i - 1] + sect[i];
    dp[i] %= MODV;
    for (auto &range : lr) {
      sect[i + range.first] = (sect[i + range.first] + dp[i]) % MODV;
      sect[i + range.second + 1] =
          ((sect[i + range.second + 1] - dp[i]) % MODV + MODV) % MODV;
    }
  }
  cout << sect[n] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define MODV 998244353
#define INFLL (1LL << 62) // 9223372036854775807
#define EPS 1e-9
#define rep(i, n) for (ll i = 0, i##_len = (ll)(n); i < i##_len; i++)
#define repf(i, n) for (ll i = 1, i##_len = (ll)(n + 1); i < i##_len; i++)
#define all(v) v.begin(), v.end()
#define endl "\\n"
#define vi vector<ll>
#define vvi vector<vector<ll>>
#define Yes() cout << "Yes" << endl
#define YES() cout << "YES" << endl
#define No() cout << "No" << endl
#define NO() cout << "NO" << endl
#define Init()                                                                 \\
  std::ios::sync_with_stdio(false);                                            \\
  std::cin.tie(0);                                                             \\
  std::cout << fixed << setprecision(15);
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;

int main() {
  Init();
  ll n, k;
  cin >> n >> k;
  vector<pair<ll, ll>> lr(k);
  rep(i, k) cin >> lr[i].first >> lr[i].second;
  sort(all(lr));

  vi dp(n + 400, 0), sect(n + 400, 0);
  dp[1] = 1, sect[1] = 1;

  for (ll i = 1; i <= n; i++) {
    dp[i] = dp[i - 1] + sect[i];
    dp[i] %= MODV;
    for (auto &range : lr) {
      sect[i + range.first] += dp[i];
      sect[i + range.first] %= MODV;
      sect[i + range.second + 1] -= dp[i];
      sect[i + range.second + 1] %= MODV;
    }
  }
  cout << sect[n] << endl;
  return 0;
}