Problem p03279 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

#define Whats(x) cout << #x << " is " << (x) << endl
#define Divhim() cout << ">>>>>>>>>>>>>>>" << endl
#define Divher() cout << "<<<<<<<<<<<<<<<" << endl
#define Oops() cout << "!!!!!!!!!!!!!!!" << endl

template <typename T> bool GetMin(T &a, T b) {
  return ((a <= b) ? false : (a = b, true));
}

template <typename T> bool GetMax(T &a, T b) {
  return ((a >= b) ? false : (a = b, true));
}

/*
         -<Unlimited Blade Works>-
 */

#define Pir pair<int, int>
#define fir first
#define sec second

const int N = 1e5;
const int MOD = 1e9 + 7;

void SU(int &x, int y) { ((x += y) >= MOD) ? (x -= MOD) : 0; }

int n, m, ax[N], ay[N];
Pir p[N];
int tot;

int ls[N], ed;

int C[N];

void Add(int x, int y) {
  for (; x <= ed; x += x & (-x))
    SU(C[x], y);
}

int Query(int x) {
  int res = 0;
  for (; x; x &= x - 1)
    SU(res, C[x]);
  return res;
}

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i)
    scanf("%d", ax + i);
  for (int i = 1; i <= m; ++i)
    scanf("%d", ay + i);
  for (int i = 1, id; i <= n; ++i) {
    if (ax[i] <= ay[1] || ax[i] >= ay[m])
      continue;
    id = lower_bound(ay + 1, ay + m + 1, ax[i]) - ay;
    if (ay[id] == ax[i])
      continue;
    p[++tot] = Pir(ax[i] - ay[id - 1], ay[id] - ax[i]);
  }
  sort(p + 1, p + tot + 1);
  tot = unique(p + 1, p + tot + 1) - p - 1;
  for (int i = 1; i <= tot; ++i) {
    ls[++ed] = p[i].fir;
    ls[++ed] = p[i].sec;
  }
  sort(ls + 1, ls + ed + 1);
  ed = unique(ls + 1, ls + ed + 1) - ls - 1;
  for (int i = 1; i <= tot; ++i) {
    p[i].fir = lower_bound(ls + 1, ls + ed + 1, p[i].fir) - ls;
    p[i].sec = lower_bound(ls + 1, ls + ed + 1, p[i].sec) - ls;
  }
  sort(p + 1, p + tot + 1, [](auto a, auto b) {
    return a.fir != b.fir ? a.fir < b.fir : a.sec > b.sec;
  });
  // for(int i=1;i<=tot;++i)
  //	cout<<p[i].fir<<","<<p[i].sec<<endl;
  int ans = 1;
  for (int i = 1, t; i <= tot; ++i) {
    t = 1;
    SU(t, Query(p[i].sec - 1));
    SU(ans, t);
    Add(p[i].sec, t);
  }
  printf("%d", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

#define Whats(x) cout << #x << " is " << (x) << endl
#define Divhim() cout << ">>>>>>>>>>>>>>>" << endl
#define Divher() cout << "<<<<<<<<<<<<<<<" << endl
#define Oops() cout << "!!!!!!!!!!!!!!!" << endl

template <typename T> bool GetMin(T &a, T b) {
  return ((a <= b) ? false : (a = b, true));
}

template <typename T> bool GetMax(T &a, T b) {
  return ((a >= b) ? false : (a = b, true));
}

/*
         -<Unlimited Blade Works>-
 */

#define Pir pair<int, int>
#define fir first
#define sec second

const int N = 5e5;
const int MOD = 1e9 + 7;

void SU(int &x, int y) { ((x += y) >= MOD) ? (x -= MOD) : 0; }

int n, m, ax[N], ay[N];
Pir p[N];
int tot;

int ls[N], ed;

int C[N];

void Add(int x, int y) {
  for (; x <= ed; x += x & (-x))
    SU(C[x], y);
}

int Query(int x) {
  int res = 0;
  for (; x; x &= x - 1)
    SU(res, C[x]);
  return res;
}

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i)
    scanf("%d", ax + i);
  for (int i = 1; i <= m; ++i)
    scanf("%d", ay + i);
  for (int i = 1, id; i <= n; ++i) {
    if (ax[i] <= ay[1] || ax[i] >= ay[m])
      continue;
    id = lower_bound(ay + 1, ay + m + 1, ax[i]) - ay;
    if (ay[id] == ax[i])
      continue;
    p[++tot] = Pir(ax[i] - ay[id - 1], ay[id] - ax[i]);
  }
  sort(p + 1, p + tot + 1);
  tot = unique(p + 1, p + tot + 1) - p - 1;
  for (int i = 1; i <= tot; ++i) {
    ls[++ed] = p[i].fir;
    ls[++ed] = p[i].sec;
  }
  sort(ls + 1, ls + ed + 1);
  ed = unique(ls + 1, ls + ed + 1) - ls - 1;
  for (int i = 1; i <= tot; ++i) {
    p[i].fir = lower_bound(ls + 1, ls + ed + 1, p[i].fir) - ls;
    p[i].sec = lower_bound(ls + 1, ls + ed + 1, p[i].sec) - ls;
  }
  sort(p + 1, p + tot + 1, [](auto a, auto b) {
    return a.fir != b.fir ? a.fir < b.fir : a.sec > b.sec;
  });
  // for(int i=1;i<=tot;++i)
  //	cout<<p[i].fir<<","<<p[i].sec<<endl;
  int ans = 1;
  for (int i = 1, t; i <= tot; ++i) {
    t = 1;
    SU(t, Query(p[i].sec - 1));
    SU(ans, t);
    Add(p[i].sec, t);
  }
  printf("%d", ans);
  return 0;
}