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

#include <algorithm>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)

using namespace std;

using ll = long long int;
using p = pair<int, int>;
using pl = pair<ll, ll>;
using v = vector<int>;
using vd = vector<double>;
using vs = vector<string>;
using vl = vector<ll>;

const int intmax = numeric_limits<int>::max();
const ll llmax = numeric_limits<ll>::max();
const ll mod = 998244353;
const double pi = M_PI;

int main() {
  int n, k;
  cin >> n >> k;
  vector<pair<int, int>> range(k);
  rep(i, k) {
    int l, r;
    cin >> l >> r;
    r++;
    range[i] = {l, r};
  }

  vl dp(n + 1, 0);
  dp[1] = 1;
  for (int i = 2; i <= n; i++) {
    dp[i] += dp[i - 1];
    dp[i] %= mod;
    for (int j = 0; j < k; j++) {
      int lindex = i - range[j].second <= 0 ? 0 : i - range[j].second;
      int rindex = i - range[j].first <= 0 ? 0 : i - range[j].first;
      ll sum = (dp[rindex] - dp[lindex]) % mod;
      if (sum < 0)
        sum += mod;
      dp[i] += sum;
      dp[i] %= mod;
    }
  }
  ll result = dp[n] - dp[n - 1];
  if (result < 0) {
    result += mod;
  }
  cout << result << endl;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)

using namespace std;
// 繧ｨ繧､繝ｪ繧｢繧ｹ
using ll = long long int;
using p = pair<int, int>;
using pl = pair<ll, ll>;
using v = vector<int>;
using vd = vector<double>;
using vs = vector<string>;
using vl = vector<ll>;

// 螳壽焚
const int intmax = numeric_limits<int>::max();
const ll llmax = numeric_limits<ll>::max();
const ll mod = 998244353;
const double pi = M_PI;

class BinaryIndexedTree {
public:
  BinaryIndexedTree(ll n);
  void add(ll i, ll x);
  ll sum(ll i);

private:
  vector<ll> nodes; // 1-indexed
  void init(ll n);
};

BinaryIndexedTree::BinaryIndexedTree(ll n) { init(n); }

void BinaryIndexedTree::init(ll n) {
  // 繝弱・繝峨・蛟､繧・縺ｫ蛻晄悄蛹・	for(ll i = 0; i < n + 1; i++)
  { nodes.push_back(0); }
}

void BinaryIndexedTree::add(ll i, ll x) {
  // i縺ｮ譛蠕後・1縺檎ｫ九▲縺ｦ縺・ｋ繝薙ャ繝医ｒ雜ｳ縺励※縺・￥
  while (i <= nodes.size()) {
    nodes[i] += x;
    i += i & -i;
  }
}

ll BinaryIndexedTree::sum(ll i) {
  ll result = 0;
  while (i > 0) {
    result = (result + nodes[i]) % mod;
    i -= i & -i;
  }
  return result;
}

int main() {
  int n, k;
  cin >> n >> k;
  vector<pair<int, int>> range(k);
  rep(i, k) {
    int l, r;
    cin >> l >> r;
    r++;
    range[i] = {l, r};
  }

  BinaryIndexedTree bit(n);
  // 蛻晄悄菴咲ｽｮ
  bit.add(1, 1);
  for (int i = 2; i <= n; i++) {
    for (int j = 0; j < k; j++) {
      int lindex = i - range[j].second <= 0 ? 0 : i - range[j].second;
      int rindex = i - range[j].first <= 0 ? 0 : i - range[j].first;
      ll sum = (bit.sum(rindex) - bit.sum(lindex)) % mod;
      cout << lindex << " " << rindex << " " << sum << endl;
      bit.add(i, sum);
    }
  }
  cout << bit.sum(n) - bit.sum(n - 1) << endl;
}