Problem p03823 (C++)

Buggy submission (status: Runtime Error):
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
template <class T> using PQ = priority_queue<T>;
template <class T> using PQG = priority_queue<T, vector<T>, greater<T>>;
const int INF = 0xccccccc;
const ll LINF = 922337203685477580LL;
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
  return is >> p.first >> p.second;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << p.first << ' ' << p.second;
}

const int mod = 1000000007;
// const int mod = 998244353;

struct mint {
  int64_t x;
  mint(int64_t x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  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 { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(int64_t 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) { return mint(*this) /= a; }
};

istream &operator>>(istream &is, mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

template <class T> struct SegTree {
  using FX = function<T(T, T)>;
  int n;
  FX fx;
  const T ex;
  vector<T> dat;
  SegTree(int n_, FX fx_, T ex_) : fx(fx_), ex(ex_), n(1) {
    while (n < n_)
      n <<= 1;
    dat.assign((n << 1) - 1, ex);
  }
  inline int chld(int k) { return (k << 1) + 1; }
  inline int chrd(int k) { return (k << 1) + 2; }
  void update(int i, T x) {
    i += n - 1;
    dat[i] = x;
    while (i) {
      i = (i - 1) >> 1;
      dat[i] = fx(dat[chld(i)], dat[chrd(i)]);
    }
  }
  inline T query(int a, int b) { return query(a, b, 0, 0, n); }
  T query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return ex;
    if (a <= l && r <= b)
      return dat[k];
    T vl = query(a, b, chld(k), l, (l + r) >> 1);
    T vr = query(a, b, chrd(k), (l + r) >> 1, r);
    return fx(vl, vr);
  }
  const T &operator[](int idx) const { return dat[idx + n - 1]; }
};

#define N 10010

// head

int n;
ll a, b;
ll s[N];
int zero;
SegTree<mint> seg(
    N, [](mint a, mint b) -> mint { return a + b; }, 0);

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  cin >> a >> b;
  rep(i, n) cin >> s[i + 1];
  s[0] = -LINF * 10;
  if (a < b)
    swap(a, b);
  int nx = 0;
  seg.update(0, 1);
  rep(i, n) {
    while (s[nx] + a <= s[i + 1])
      nx++;
    seg.update(i + 1, seg.query(0, nx));
    if (i) {
      if (s[i - 1] + b > s[i + 1]) {
        cout << 0 << endl;
        return 0;
      }
    }
    if (s[i] + b > s[i + 1]) {
      for (int &j = zero; j < i; j++) {
        seg.update(j, 0);
      }
    }
  }
  cout << seg.query(0, n + 1) << endl;
}

Fixed submission:
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
template <class T> using PQ = priority_queue<T>;
template <class T> using PQG = priority_queue<T, vector<T>, greater<T>>;
const int INF = 0xccccccc;
const ll LINF = 922337203685477580LL;
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
  return is >> p.first >> p.second;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << p.first << ' ' << p.second;
}

const int mod = 1000000007;
// const int mod = 998244353;

struct mint {
  int64_t x;
  mint(int64_t x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  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 { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(int64_t 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) { return mint(*this) /= a; }
};

istream &operator>>(istream &is, mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

template <class T> struct SegTree {
  using FX = function<T(T, T)>;
  int n;
  FX fx;
  const T ex;
  vector<T> dat;
  SegTree(int n_, FX fx_, T ex_) : fx(fx_), ex(ex_), n(1) {
    while (n < n_)
      n <<= 1;
    dat.assign((n << 1) - 1, ex);
  }
  inline int chld(int k) { return (k << 1) + 1; }
  inline int chrd(int k) { return (k << 1) + 2; }
  void update(int i, T x) {
    i += n - 1;
    dat[i] = x;
    while (i) {
      i = (i - 1) >> 1;
      dat[i] = fx(dat[chld(i)], dat[chrd(i)]);
    }
  }
  inline T query(int a, int b) { return query(a, b, 0, 0, n); }
  T query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return ex;
    if (a <= l && r <= b)
      return dat[k];
    T vl = query(a, b, chld(k), l, (l + r) >> 1);
    T vr = query(a, b, chrd(k), (l + r) >> 1, r);
    return fx(vl, vr);
  }
  const T &operator[](int idx) const { return dat[idx + n - 1]; }
};

#define N 100100

// head

int n;
ll a, b;
ll s[N];
int zero;
SegTree<mint> seg(
    N, [](mint a, mint b) -> mint { return a + b; }, 0);

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  cin >> a >> b;
  rep(i, n) cin >> s[i + 1];
  s[0] = -LINF * 10;
  if (a < b)
    swap(a, b);
  int nx = 0;
  seg.update(0, 1);
  rep(i, n) {
    while (s[nx] + a <= s[i + 1])
      nx++;
    seg.update(i + 1, seg.query(0, nx));
    if (i) {
      if (s[i - 1] + b > s[i + 1]) {
        cout << 0 << endl;
        return 0;
      }
    }
    if (s[i] + b > s[i + 1]) {
      for (int &j = zero; j < i; j++) {
        seg.update(j, 0);
      }
    }
  }
  cout << seg.query(0, n + 1) << endl;
}