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

#include <bits/stdc++.h>
using namespace std;

#define d(x) cerr << #x ":" << x << endl;
#define dd(x, y) cerr << "(" #x "," #y "):(" << x << "," << y << ")" << endl
#define rep(i, n) for (int i = (int)(0); i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define dump(v)                                                                \\
  cerr << #v ":[ ";                                                            \\
  for (auto macro_vi : v) {                                                    \\
    cerr << macro_vi << " ";                                                   \\
  }                                                                            \\
  cerr << "]" << endl;
#define ddump(v)                                                               \\
  cerr << #v ":" << endl;                                                      \\
  for (auto macro_row : v) {                                                   \\
    cerr << "[";                                                               \\
    for (auto macro__vi : macro_row) {                                         \\
      cerr << macro__vi << " ";                                                \\
    }                                                                          \\
    cerr << "]" << endl;                                                       \\
  }
using lint = long long;
const int INF = 1e9;
const lint LINF = 1e18;
const double EPS = 1e-10;

const int mod = 998244353;
struct mint {
  long long x;
  mint(long long x = 0) : x((x % mod + mod) % mod) {}
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }
  mint pow(long long t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return (*this) *= a.inv(); }
  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
};

int main() {
  lint N, K;
  cin >> N >> K;
  vector<lint> L(K), R(K);
  rep(i, K) { cin >> L[i] >> R[i]; }

  vector<mint> cnt(N + 3, 0);
  cnt[1] = 1;

  for (int i = 1; i < N + 1; i++) {

    for (int k = 0; k < K; k++) {
      int left = i + L[k], right = min(N + 1, i + R[k]);
      if (N < left) {
        continue;
      }
      cnt[left] += cnt[i];
      cnt[right + 1] -= cnt[i];
    }

    cnt[i + 2] += cnt[i + 1];
  }

  cout << cnt[N].x << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define d(x) cerr << #x ":" << x << endl;
#define dd(x, y) cerr << "(" #x "," #y "):(" << x << "," << y << ")" << endl
#define rep(i, n) for (int i = (int)(0); i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define dump(v)                                                                \\
  cerr << #v ":[ ";                                                            \\
  for (auto macro_vi : v) {                                                    \\
    cerr << macro_vi << " ";                                                   \\
  }                                                                            \\
  cerr << "]" << endl;
#define ddump(v)                                                               \\
  cerr << #v ":" << endl;                                                      \\
  for (auto macro_row : v) {                                                   \\
    cerr << "[";                                                               \\
    for (auto macro__vi : macro_row) {                                         \\
      cerr << macro__vi << " ";                                                \\
    }                                                                          \\
    cerr << "]" << endl;                                                       \\
  }
using lint = long long;
const int INF = 1e9;
const lint LINF = 1e18;
const double EPS = 1e-10;

const int mod = 998244353;
struct mint {
  long long x;
  mint(long long x = 0) : x((x % mod + mod) % mod) {}
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }
  mint pow(long long t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return (*this) *= a.inv(); }
  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
};

int main() {
  lint N, K;
  cin >> N >> K;
  vector<lint> L(K), R(K);
  rep(i, K) { cin >> L[i] >> R[i]; }

  vector<mint> cnt(N + 3, 0);
  cnt[1] = 1;

  for (int i = 1; i < N + 1; i++) {

    for (int k = 0; k < K; k++) {
      int left = i + L[k], right = min(N + 1, i + R[k]);
      cnt[left] += cnt[i];
      cnt[right + 1] -= cnt[i];
    }

    cnt[i + 2] += cnt[i + 1];
  }

  cout << cnt[N].x << endl;

  return 0;
}