Problem p03709 (C++)

Buggy submission (status: Runtime Error):
// Code by ajcxsu
// Problem: river correction

#include <bits/stdc++.h>
#define MOD (1000000007)
using namespace std;

const int N = 5e5 + 10;
int h[N], l[N], r[N];
vector<int> L[N];
int x[N], v[N], a[N];
bool cmp(const int &a, const int &b) { return v[a] < v[b]; }
int f[N], S[N];

int main() {
  freopen("river.in", "r", stdin);
  freopen("river.out", "w", stdout);
  ios::sync_with_stdio(false), cin.tie(0);
  int n, m;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> x[i] >> v[i], a[i] = i;
  sort(a + 1, a + 1 + n, cmp);
  int stk[N], p[N], t = 0;
  for (int i = 1; i <= n; i++) {
    if (!t || stk[t] < x[a[i]])
      stk[++t] = x[a[i]], p[t] = i;
    int l = 1, r = t, mid;
    while (l < r) {
      mid = (l + r) >> 1;
      if (stk[mid] < x[a[i]])
        l = mid + 1;
      else
        r = mid;
    }
    ::l[i] = p[r];
  }
  t = 0;
  for (int i = n; i >= 1; i--) {
    if (!t || stk[t] > x[a[i]])
      stk[++t] = x[a[i]], p[t] = i;
    int l = 1, r = t, mid;
    while (l < r) {
      mid = (l + r) >> 1;
      if (stk[mid] > x[a[i]])
        l = mid + 1;
      else
        r = mid;
    }
    ::r[i] = p[r];
  }
  // cin>>m;
  // for(int i=1; i<=n; i++) cin>>l[i]>>r[i];
  for (int i = 1; i <= n; i++)
    L[r[i]].push_back(l[i]);
  f[0] = S[0] = 1;
  for (int i = 1; i <= n; i++) {
    S[i] = S[i - 1];
    sort(L[i].begin(), L[i].end());
    for (int j : L[i]) {
      f[i] = (1ll * f[i] + S[i] - (j - 2 >= 0 ? S[j - 2] : 0) + MOD) % MOD;
      S[i] = (f[i] + S[i - 1]) % MOD;
    }
    // cout<<f[i]<<" \n"[i==n];
  }
  cout << f[n] << endl;
  return 0;
}

Fixed submission:
// Code by ajcxsu
// Problem: river correction

#include <bits/stdc++.h>
#define MOD (1000000007)
using namespace std;

const int N = 5e5 + 10;
int h[N], l[N], r[N];
vector<int> L[N];
int x[N], v[N], a[N];
bool cmp(const int &a, const int &b) { return v[a] < v[b]; }
int f[N], S[N];

int main() {
  // freopen("river.in","r",stdin);
  // freopen("river.out","w",stdout);
  ios::sync_with_stdio(false), cin.tie(0);
  int n, m;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> x[i] >> v[i], a[i] = i;
  sort(a + 1, a + 1 + n, cmp);
  int stk[N], p[N], t = 0;
  for (int i = 1; i <= n; i++) {
    if (!t || stk[t] < x[a[i]])
      stk[++t] = x[a[i]], p[t] = i;
    int l = 1, r = t, mid;
    while (l < r) {
      mid = (l + r) >> 1;
      if (stk[mid] < x[a[i]])
        l = mid + 1;
      else
        r = mid;
    }
    ::l[i] = p[r];
  }
  t = 0;
  for (int i = n; i >= 1; i--) {
    if (!t || stk[t] > x[a[i]])
      stk[++t] = x[a[i]], p[t] = i;
    int l = 1, r = t, mid;
    while (l < r) {
      mid = (l + r) >> 1;
      if (stk[mid] > x[a[i]])
        l = mid + 1;
      else
        r = mid;
    }
    ::r[i] = p[r];
  }
  // cin>>m;
  // for(int i=1; i<=n; i++) cin>>l[i]>>r[i];
  for (int i = 1; i <= n; i++)
    L[r[i]].push_back(l[i]);
  f[0] = S[0] = 1;
  for (int i = 1; i <= n; i++) {
    S[i] = S[i - 1];
    sort(L[i].begin(), L[i].end());
    for (int j : L[i]) {
      f[i] = (1ll * f[i] + S[i] - (j - 2 >= 0 ? S[j - 2] : 0) + MOD) % MOD;
      S[i] = (f[i] + S[i - 1]) % MOD;
    }
    // cout<<f[i]<<" \n"[i==n];
  }
  cout << f[n] << endl;
  return 0;
}