Problem p02549 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define fast_io                                                                \
  cin.tie(0);                                                                  \
  ios_base::sync_with_stdio(0);
string to_string(string s) { return '"' + s + '"'; }
string to_string(char s) { return string(1, s); }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A> string to_string(A);
template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A> string to_string(A v) {
  bool f = 1;
  string r = "{";
  for (const auto &x : v) {
    if (!f)
      r += ", ";
    f = 0;
    r += to_string(x);
  }
  return r + "}";
}
void debug_out() { cout << endl; }
void show() { cout << endl; }
void pret() {
  cout << endl;
  exit(0);
}
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cout << " " << to_string(H);
  debug_out(T...);
}
template <typename Head, typename... Tail> void show(Head H, Tail... T) {
  cout << H << " ";
  show(T...);
}
template <typename Head, typename... Tail> void pret(Head H, Tail... T) {
  cout << H << " ";
  pret(T...);
}
#define pr(...) cout << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
typedef long long ll;
#define int ll
typedef long double ld;
typedef vector<int> vi;
#define disp(x) cout << x << " ";
#define rep(i, a, b) for (int i = a; i < (int)b; i++)
#define fo(i, a, b) for (int i = a; i <= (int)b; i++)
#define rf(i, a, b) for (int i = a; i >= (int)b; i--)
#define mp make_pair
#define pb emplace_back
#define F first
#define S second
#define endl '\n'
// cout.setf(ios::fixed);cout.precision(18)
const int MOD = 998244353;
const int maxn = 3e5 + 10;

int dp[maxn];

void add(int l, int r, int x) {
  dp[l] += x;
  dp[l] %= MOD;
  if (r >= maxn)
    return;
  dp[r + 1] -= x;
  dp[r + 1] %= MOD;
}

int32_t main() {
  fast_io;
  int n, k;
  cin >> n >> k;
  vector<pair<int, int>> v;
  for (int i = 0; i < k; ++i) {
    int l, r;
    cin >> l >> r;
    v.emplace_back(l, r);
  }
  add(1, 1, 1);
  fo(i, 1, n) {
    dp[i] += dp[i - 1];
    dp[i] %= MOD;
    // show(i, dp[i]);
    for (const auto &it : v) {
      add(i + it.first, i + it.second, dp[i]);
    }
  }
  if (dp[n] < 0)
    dp[n] += MOD;
  show(dp[n]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fast_io                                                                \
  cin.tie(0);                                                                  \
  ios_base::sync_with_stdio(0);
string to_string(string s) { return '"' + s + '"'; }
string to_string(char s) { return string(1, s); }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A> string to_string(A);
template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A> string to_string(A v) {
  bool f = 1;
  string r = "{";
  for (const auto &x : v) {
    if (!f)
      r += ", ";
    f = 0;
    r += to_string(x);
  }
  return r + "}";
}
void debug_out() { cout << endl; }
void show() { cout << endl; }
void pret() {
  cout << endl;
  exit(0);
}
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cout << " " << to_string(H);
  debug_out(T...);
}
template <typename Head, typename... Tail> void show(Head H, Tail... T) {
  cout << H << " ";
  show(T...);
}
template <typename Head, typename... Tail> void pret(Head H, Tail... T) {
  cout << H << " ";
  pret(T...);
}
#define pr(...) cout << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
typedef long long ll;
#define int ll
typedef long double ld;
typedef vector<int> vi;
#define disp(x) cout << x << " ";
#define rep(i, a, b) for (int i = a; i < (int)b; i++)
#define fo(i, a, b) for (int i = a; i <= (int)b; i++)
#define rf(i, a, b) for (int i = a; i >= (int)b; i--)
#define mp make_pair
#define pb emplace_back
#define F first
#define S second
#define endl '\n'
// cout.setf(ios::fixed);cout.precision(18)
const int MOD = 998244353;
const int maxn = 3e5 + 10;

int dp[maxn];

void add(int l, int r, int x) {
  if (l >= maxn)
    return;
  dp[l] += x;
  dp[l] %= MOD;
  if (r >= maxn)
    return;
  dp[r + 1] -= x;
  dp[r + 1] %= MOD;
}

int32_t main() {
  fast_io;
  int n, k;
  cin >> n >> k;
  vector<pair<int, int>> v;
  for (int i = 0; i < k; ++i) {
    int l, r;
    cin >> l >> r;
    v.emplace_back(l, r);
  }
  add(1, 1, 1);
  fo(i, 1, n) {
    dp[i] += dp[i - 1];
    dp[i] %= MOD;
    // show(i, dp[i]);
    for (const auto &it : v) {
      add(i + it.first, i + it.second, dp[i]);
    }
  }
  if (dp[n] < 0)
    dp[n] += MOD;
  show(dp[n]);
  return 0;
}