Problem p02549 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n - 1); i >= 0; i--)
#define all(x) x.begin(), x.end()
#define br cout << "\n";
using namespace std;
const long long INF = 1e10;
const long long MOD = 1e9 + 7;
using Graph = vector<vector<ll>>;
using pll = pair<ll, ll>;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
ll ceilll(ll a, ll b) { return (a + b - 1) / b; } // if(a%b == 0) (a/b) + 1
ll get_digit(ll a) {
  ll digit = 0;
  while (a != 0) {
    a /= 10;
    digit++;
  }
  return digit;
} // a != 0
template <typename T> void vecdbg(vector<T> &v) {
  rep(i, v.size()) { cout << v[i] << " "; }
  br;
}
template <typename T> void vecvecdbg(vector<vector<T>> &v) {
  rep(i, v.size()) {
    rep(j, v[i].size()) { cout << v[i][j] << " "; }
    br;
  }
}

// 0 false, 1 true
// string number to int : -48
// a to A : -32
// ceil(a)  1.2->2.0
// c++17	g++ -std=c++17 a.cpp
// global vector -> 0 initialization

// https://atcoder.jp/contests/abc172/submissions/14765570
// 20200724
template <long long mod> struct ModInt {
  long long x;
  ModInt() : x(0) {}
  ModInt(long long x) : x(x >= 0 ? x % mod : (mod - -x % mod) % mod) {}
  ModInt &operator+=(const ModInt &p) {
    if ((x += p.x) >= mod)
      x -= mod;
    return *this;
  }
  ModInt &operator-=(const ModInt &p) {
    if ((x += mod - p.x) >= mod)
      x -= mod;
    return *this;
  }
  ModInt &operator*=(const ModInt &p) {
    x = (long long)x * p.x % mod;
    return *this;
  }
  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }
  ModInt &operator^=(long long p) {
    ModInt res = 1;
    for (; p; p >>= 1) {
      if (p & 1)
        res *= *this;
      *this *= *this;
    }
    return *this = res;
  }
  ModInt operator-() const { return ModInt(-x); }
  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
  ModInt operator^(long long p) const { return ModInt(*this) ^= p; }
  bool operator==(const ModInt &p) const { return x == p.x; }
  bool operator!=(const ModInt &p) const { return x != p.x; }
  explicit operator long long() const { return x; }
  ModInt &operator=(const long long p) {
    x = p;
    return *this;
  }
  ModInt inverse() const {
    long long a = x, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      a -= t * b;
      std::swap(a, b);
      u -= t * v;
      std::swap(u, v);
    }
    return ModInt(u);
  }
  friend std::ostream &operator<<(std::ostream &stream, const ModInt<mod> &p) {
    return stream << p.x;
  }
  friend std::istream &operator>>(std::istream &stream, ModInt<mod> &a) {
    long long x;
    stream >> x;
    a = ModInt<mod>(x);
    return stream;
  }
};

template <long long mod> struct MComb {
  using mint = ModInt<mod>;
  std::vector<mint> fact;
  std::vector<mint> inv;
  MComb(long long n) { // O(n + log(mod))
    fact = std::vector<mint>(n + 1, 1);
    for (long long i = 1; i <= n; i++)
      fact[i] = fact[i - 1] * mint(i);
    inv.resize(n + 1);
    inv[n] = fact[n] ^ (mod - 2);
    for (long long i = n; i--;)
      inv[i] = inv[i + 1] * mint(i + 1);
  }
  mint ncr(long long n, long r) { return fact[n] * inv[r] * inv[n - r]; }
  mint npr(long n, long r) { return fact[n] * inv[n - r]; }
  mint nhr(long n, long r) {
    assert(n + r - 1 < (long long)fact.size());
    return ncr(n + r - 1, r);
  }
};

template <long long mod> struct MPow {
  using mint = ModInt<mod>;
  mint modpow(mint a, long long n) {
    mint res = 1;
    while (n > 0) {
      if (n & 1) {
        res = res * a;
      }
      a = a * a;
      n >>= 1;
    }
    return res;
  }
};

typedef ModInt<998244353> mint;
MComb<998244353> com(510000);
MPow<998244353> mpow;

mint dp[200010];
mint dp_sum[200010];

int main() {
  std::cout << std::fixed << std::setprecision(15);
  ll n, k;
  cin >> n >> k;
  vector<ll> vec;
  vector<pair<ll, ll>> pv;
  rep(i, k) {
    ll l, r;
    cin >> l >> r;
    pv.emplace_back(l, r);
    /*
    for(ll j = l; j <= r; j++){
        vec.push_back(j);

    }
    */
  }

  sort(all(pv));

  rep(i, pv.size()) {
    for (ll j = pv[i].first; j <= pv[i].second; j++) {
      vec.push_back(j);
    }
  }

  // sort(all(vec));
  /*
  rep(i, vec.size()){
      cout << vec[i] << " ";
  }
  br;
  */

  dp[1] = 1;
  dp_sum[1] = 1;
  for (ll i = 1; i < n + 1; i++) {
    for (ll j = 0; j < pv.size(); j++) {
      if (i - pv[j].first < 0)
        continue;
      if (i - pv[i].second < 1) {
        dp[i] += (dp_sum[i - pv[j].first] - dp_sum[0]);
      } else {
        dp[i] += (dp_sum[i - pv[j].first] - dp_sum[i - pv[j].second - 1]);
      }
    }

    /*
    for(ll j = 0; j < vec.size(); j++){
        if(i - vec[j] < 0){
            //cout << i << " " << j << " c" << endl;
            break;
        }
        dp[i] += dp[i-vec[j]];
    }
    */
    dp_sum[i] = dp_sum[i - 1] + dp[i];
  }

  /*
  rep(i, n+1){
      cout << dp[i] << " ";
  }
  br;
  rep(i, n+1){
      cout << dp_sum[i] << " ";
  }
  br;
  */

  cout << dp[n] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n - 1); i >= 0; i--)
#define all(x) x.begin(), x.end()
#define br cout << "\n";
using namespace std;
const long long INF = 1e10;
const long long MOD = 1e9 + 7;
using Graph = vector<vector<ll>>;
using pll = pair<ll, ll>;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
ll ceilll(ll a, ll b) { return (a + b - 1) / b; } // if(a%b == 0) (a/b) + 1
ll get_digit(ll a) {
  ll digit = 0;
  while (a != 0) {
    a /= 10;
    digit++;
  }
  return digit;
} // a != 0
template <typename T> void vecdbg(vector<T> &v) {
  rep(i, v.size()) { cout << v[i] << " "; }
  br;
}
template <typename T> void vecvecdbg(vector<vector<T>> &v) {
  rep(i, v.size()) {
    rep(j, v[i].size()) { cout << v[i][j] << " "; }
    br;
  }
}

// 0 false, 1 true
// string number to int : -48
// a to A : -32
// ceil(a)  1.2->2.0
// c++17	g++ -std=c++17 a.cpp
// global vector -> 0 initialization

// https://atcoder.jp/contests/abc172/submissions/14765570
// 20200724
template <long long mod> struct ModInt {
  long long x;
  ModInt() : x(0) {}
  ModInt(long long x) : x(x >= 0 ? x % mod : (mod - -x % mod) % mod) {}
  ModInt &operator+=(const ModInt &p) {
    if ((x += p.x) >= mod)
      x -= mod;
    return *this;
  }
  ModInt &operator-=(const ModInt &p) {
    if ((x += mod - p.x) >= mod)
      x -= mod;
    return *this;
  }
  ModInt &operator*=(const ModInt &p) {
    x = (long long)x * p.x % mod;
    return *this;
  }
  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }
  ModInt &operator^=(long long p) {
    ModInt res = 1;
    for (; p; p >>= 1) {
      if (p & 1)
        res *= *this;
      *this *= *this;
    }
    return *this = res;
  }
  ModInt operator-() const { return ModInt(-x); }
  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
  ModInt operator^(long long p) const { return ModInt(*this) ^= p; }
  bool operator==(const ModInt &p) const { return x == p.x; }
  bool operator!=(const ModInt &p) const { return x != p.x; }
  explicit operator long long() const { return x; }
  ModInt &operator=(const long long p) {
    x = p;
    return *this;
  }
  ModInt inverse() const {
    long long a = x, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      a -= t * b;
      std::swap(a, b);
      u -= t * v;
      std::swap(u, v);
    }
    return ModInt(u);
  }
  friend std::ostream &operator<<(std::ostream &stream, const ModInt<mod> &p) {
    return stream << p.x;
  }
  friend std::istream &operator>>(std::istream &stream, ModInt<mod> &a) {
    long long x;
    stream >> x;
    a = ModInt<mod>(x);
    return stream;
  }
};

template <long long mod> struct MComb {
  using mint = ModInt<mod>;
  std::vector<mint> fact;
  std::vector<mint> inv;
  MComb(long long n) { // O(n + log(mod))
    fact = std::vector<mint>(n + 1, 1);
    for (long long i = 1; i <= n; i++)
      fact[i] = fact[i - 1] * mint(i);
    inv.resize(n + 1);
    inv[n] = fact[n] ^ (mod - 2);
    for (long long i = n; i--;)
      inv[i] = inv[i + 1] * mint(i + 1);
  }
  mint ncr(long long n, long r) { return fact[n] * inv[r] * inv[n - r]; }
  mint npr(long n, long r) { return fact[n] * inv[n - r]; }
  mint nhr(long n, long r) {
    assert(n + r - 1 < (long long)fact.size());
    return ncr(n + r - 1, r);
  }
};

template <long long mod> struct MPow {
  using mint = ModInt<mod>;
  mint modpow(mint a, long long n) {
    mint res = 1;
    while (n > 0) {
      if (n & 1) {
        res = res * a;
      }
      a = a * a;
      n >>= 1;
    }
    return res;
  }
};

typedef ModInt<998244353> mint;
MComb<998244353> com(510000);
MPow<998244353> mpow;

mint dp[200010];
mint dp_sum[200010];

int main() {
  std::cout << std::fixed << std::setprecision(15);
  ll n, k;
  cin >> n >> k;
  vector<ll> vec;
  vector<pair<ll, ll>> pv;
  rep(i, k) {
    ll l, r;
    cin >> l >> r;
    pv.emplace_back(l, r);
    /*
    for(ll j = l; j <= r; j++){
        vec.push_back(j);

    }
    */
  }

  sort(all(pv));

  rep(i, pv.size()) {
    for (ll j = pv[i].first; j <= pv[i].second; j++) {
      vec.push_back(j);
    }
  }

  // sort(all(vec));
  /*
  rep(i, vec.size()){
      cout << vec[i] << " ";
  }
  br;
  */

  dp[1] = 1;
  dp_sum[1] = 1;
  for (ll i = 1; i < n + 1; i++) {
    for (ll j = 0; j < pv.size(); j++) {
      if (i - pv[j].first < 0)
        continue;
      if (i - pv[j].second < 1) {
        dp[i] += (dp_sum[i - pv[j].first] - dp_sum[0]);
      } else {
        dp[i] += (dp_sum[i - pv[j].first] - dp_sum[i - pv[j].second - 1]);
      }
    }

    /*
    for(ll j = 0; j < vec.size(); j++){
        if(i - vec[j] < 0){
            //cout << i << " " << j << " c" << endl;
            break;
        }
        dp[i] += dp[i-vec[j]];
    }
    */
    dp_sum[i] = dp_sum[i - 1] + dp[i];
  }

  /*
  rep(i, n+1){
      cout << dp[i] << " ";
  }
  br;
  rep(i, n+1){
      cout << dp_sum[i] << " ";
  }
  br;
  */

  cout << dp[n] << endl;
}