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

#include <bits/stdc++.h>
#include <math.h>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define M_PI 3.14159265358979323846

const int M = 998244353;
long long mod(long long x) { return ((x % M + M) % M); }
long long add(long long a, long long b) { return mod(mod(a) + mod(b)); }
long long mul(long long a, long long b) { return mod(mod(a) * mod(b)); }
long long mins(long long a, long long b) { return mod(mod(a) - mod(b)); }

ll modPow(ll a, ll b) {
  if (b == 0)
    return 1LL;
  if (b == 1)
    return a % M;
  ll res = 1;
  while (b) {
    if (b % 2 == 1)
      res = mul(res, a);
    a = mul(a, a);
    b = b / 2;
  }
  return res;
}
vector<ll> a(200005);
vector<pair<ll, ll>> seg(12);
void solve() {
  ll n, k;
  cin >> n >> k;
  for (int i = 0; i < k; i++) {
    cin >> seg[i].first >> seg[i].second;
  }
  vector<ll> dp(n + 1), psum(n + 1);
  psum[1] = 1;
  for (int i = 1; i <= n; i++) {
    if (i > 2)
      psum[i] = add(psum[i], psum[i - 1]);
    psum[i] = add(psum[i], dp[i]);
    for (int j = 0; j < k; j++) {
      if (i + seg[j].first <= n)
        dp[i + seg[j].first] = add(dp[i + seg[j].first], psum[i]);
      if (i + seg[j].second + 1 <= n)
        dp[i + seg[j].second + 1] = mins(dp[i + seg[j].second + 1], psum[i]);
    }
  }
  cout << psum[n];
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed;
  cout << setprecision(10);
  //        freopen("timber_input.txt", "r", stdin);
  //        freopen("timber_output.txt", "w", stdout);
  int t = 1;
  // cin>>t;
  for (int i = 1; i <= t; i++) {
    //            cout<<"Case #"<<i<<": ";
    solve();
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <math.h>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define M_PI 3.14159265358979323846

const int M = 998244353;
long long mod(long long x) { return ((x % M + M) % M); }
long long add(long long a, long long b) { return mod(mod(a) + mod(b)); }
long long mul(long long a, long long b) { return mod(mod(a) * mod(b)); }

ll modPow(ll a, ll b) {
  if (b == 0)
    return 1LL;
  if (b == 1)
    return a % M;
  ll res = 1;
  while (b) {
    if (b % 2 == 1)
      res = mul(res, a);
    a = mul(a, a);
    b = b / 2;
  }
  return res;
}
vector<ll> a(200005);
vector<pair<ll, ll>> seg(12);
void solve() {
  ll n, k;
  cin >> n >> k;
  for (int i = 1; i <= k; i++) {
    cin >> seg[i].first >> seg[i].second;
  }
  for (int i = 1; i <= k; i++) {
    a[seg[i].first + 1]++;
    a[seg[i].second + 1 + 1]--;
  }
  ll total = 0;
  for (int i = 2; i < n; i++) {
    total = add(total, a[i]);
    for (int j = 1; j <= k; j++) {
      a[seg[j].first + i] += total;
      a[seg[j].second + i + 1] -= total;
    }
  }
  cout << add(total, a[n]);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed;
  cout << setprecision(10);
  //        freopen("timber_input.txt", "r", stdin);
  //        freopen("timber_output.txt", "w", stdout);
  int t = 1;
  // cin>>t;
  for (int i = 1; i <= t; i++) {
    //            cout<<"Case #"<<i<<": ";
    solve();
  }
  return 0;
}