Problem p03051 (C++)

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

#define fo(i, j, l) for (int i = j; i <= l; ++i)
#define fd(i, j, l) for (int i = j; i >= l; --i)

using namespace std;
typedef long long ll;
const ll N = 12e4, mo = 1e9 + 7;

int a[N], n, b[N], c[N], d[N];
int qz[N], hz[N];
int lx[N];
int bh[N];

ll f[N];

inline bool kmp(int x, int y) {
  return (c[x] != c[y]) ? c[x] < c[y]
                        : ((b[x] != b[y]) ? (b[x] > b[y]) : (a[x] < a[y]));
}

int main() {
  //	freopen("a.in","r",stdin);
  //	freopen("a.out","w",stdout);
  scanf("%d", &n);
  fo(i, 1, n) scanf("%d", &a[i]);
  fo(i, 1, n) qz[i] = qz[i - 1] ^ a[i];
  fd(i, n, 1) hz[i] = hz[i + 1] ^ a[i];
  fd(i, n, 1) lx[i] = lx[i + 1] + (hz[i] == 0);
  fo(i, 1, n * 2) bh[i] = i;
  fo(i, 1, n) a[i] = 1, b[i] = i, c[i] = qz[i];
  fo(i, 1, n) a[i + n] = 2, b[i + n] = i, c[i + n] = hz[i];
  sort(bh + 1, bh + n + n + 1, kmp);
  int j = 0;
  b[0] = -1;
  ll ans = 1;
  fd(i, n * 2, 1) if (c[bh[i]] != 0) j = i - 1;
  fo(i, 1, n * 2) if (c[bh[i]] != c[bh[i + 1]] && c[bh[i]] != 0) {
    fo(l, j + 1, i) d[l - j] = bh[l];
    int cd = i - j, sz = c[bh[i]];
    ll l1 = 0, l2 = 0, k1 = 0, k2 = 0;
    int las = n + 1;
    fo(l, 1, cd) if (a[d[l]] == 1) {
      int po = b[d[l]] + 1;
      if (hz[po] != 0 && hz[po] != sz)
        continue;
      if (hz[po] == 0)
        ans = (ans + k2) % mo;
      if (hz[po] == sz)
        ans = (ans + k1) % mo;
    }
    else {
      int po = b[d[l]];
      ll j2 = (l2 + l1 * (lx[po] - lx[las])) % mo;
      k1 = (1 + j2) % mo;
      ll j1 = (l1 + j2 + 1) % mo;
      k2 = j1;
      l2 = j2;
      l1 = j1;
      las = po;
    }
    j = i;
  }
  int gs = 0;
  if (qz[n] == 0) {
    fo(i, 1, n - 1) if (qz[i] == 0)++ gs;
    ll lj = 1;
    fo(l, 1, gs) lj = lj * 2 % mo;
    ans = (ans + lj - 1) % mo;
  }
  cout << ans;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>

#define fo(i, j, l) for (int i = j; i <= l; ++i)
#define fd(i, j, l) for (int i = j; i >= l; --i)

using namespace std;
typedef long long ll;
const ll N = 12e5, mo = 1e9 + 7;

int a[N], n, b[N], c[N], d[N];
int qz[N], hz[N];
int lx[N];
int bh[N];

ll f[N];

inline bool kmp(int x, int y) {
  return (c[x] != c[y]) ? c[x] < c[y]
                        : ((b[x] != b[y]) ? (b[x] > b[y]) : (a[x] < a[y]));
}

int main() {
  //	freopen("a.in","r",stdin);
  //	freopen("a.out","w",stdout);
  scanf("%d", &n);
  fo(i, 1, n) scanf("%d", &a[i]);
  fo(i, 1, n) qz[i] = qz[i - 1] ^ a[i];
  fd(i, n, 1) hz[i] = hz[i + 1] ^ a[i];
  fd(i, n, 1) lx[i] = lx[i + 1] + (hz[i] == 0);
  fo(i, 1, n * 2) bh[i] = i;
  fo(i, 1, n) a[i] = 1, b[i] = i, c[i] = qz[i];
  fo(i, 1, n) a[i + n] = 2, b[i + n] = i, c[i + n] = hz[i];
  sort(bh + 1, bh + n + n + 1, kmp);
  int j = 0;
  b[0] = -1;
  ll ans = 1;
  fd(i, n * 2, 1) if (c[bh[i]] != 0) j = i - 1;
  fo(i, 1, n * 2) if (c[bh[i]] != c[bh[i + 1]] && c[bh[i]] != 0) {
    fo(l, j + 1, i) d[l - j] = bh[l];
    int cd = i - j, sz = c[bh[i]];
    ll l1 = 0, l2 = 0, k1 = 0, k2 = 0;
    int las = n + 1;
    fo(l, 1, cd) if (a[d[l]] == 1) {
      int po = b[d[l]] + 1;
      if (hz[po] != 0 && hz[po] != sz)
        continue;
      if (hz[po] == 0)
        ans = (ans + k2) % mo;
      if (hz[po] == sz)
        ans = (ans + k1) % mo;
    }
    else {
      int po = b[d[l]];
      ll j2 = (l2 + l1 * (lx[po] - lx[las])) % mo;
      k1 = (1 + j2) % mo;
      ll j1 = (l1 + j2 + 1) % mo;
      k2 = j1;
      l2 = j2;
      l1 = j1;
      las = po;
    }
    j = i;
  }
  int gs = 0;
  if (qz[n] == 0) {
    fo(i, 1, n - 1) if (qz[i] == 0)++ gs;
    ll lj = 1;
    fo(l, 1, gs) lj = lj * 2 % mo;
    ans = (ans + lj - 1) % mo;
  }
  cout << ans;
}