Problem p03051 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define PII pair<int, int>
#define LL long long
using namespace std;
const int MAXN = 500005;
const LL INF = (LL)1e9 + 5;
const LL MOD = 1000000007;

LL N, a[MAXN], sum[MAXN], dp[MAXN], ans;
LL dp_sum[1 << 20], cntz[MAXN], pre[MAXN];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> a[i];
    sum[i] = sum[i - 1] ^ a[i];
  }

  dp[0] = 1;
  for (int i = 1; i <= N; i++) {
    if (pre[sum[i]] == 0) {
      dp[i] = 1;
    } else {
      dp[i] = ((dp[pre[sum[i]]] - 1) +
               dp_sum[sum[i]] * (cntz[i - 1] - cntz[pre[sum[i]] - 1])) %
              MOD;
      //			cout << "Add " << dp_sum[sum[i]] * (cntz[i - 1]
      //- cntz[pre[sum[i]] - 1]) << ' ' << dp[pre[sum[i]]] - 1 << '\n'; 			cout <<
      //"Before " << i << " get " << dp[i] << '\n';
      if (sum[i] == 0)
        dp[i] = (dp[i] - dp[pre[sum[i]]]) % MOD;
      //			cout << "After " << i << " get " << dp[i] <<
      //'\n';
      dp[i] = (dp[i] + 1) % MOD;
      //			dp[i] = 1;
      //			for (int j = 1; j < i; j++) {
      //				if (sum[j] == sum[i]) {
      //					for (int k = j + 1; k < i; k++)
      //{ 						if (sum[k] == 0) { 							dp[i] = (dp[i] + dp[j]) % MOD;
      //						}
      //					}
      //				}
      //			}
    }

    pre[sum[i]] = i;
    cntz[i] = cntz[i - 1] + (sum[i] == 0);
    dp_sum[sum[i]] = (dp_sum[sum[i]] + dp[i]) % MOD;
    if (sum[N] == 0)
      ans += dp[i];
    //		cout << "At " << i << " get " << dp[i] << '\n';
  }

  if (sum[N] != 0)
    ans = dp[N];
  cout << (ans % MOD + MOD) % MOD << '\n';

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define PII pair<int, int>
#define LL long long
using namespace std;
const int MAXN = 500005;
const LL INF = (LL)1e9 + 5;
const LL MOD = 1000000007;

LL N, a[MAXN], sum[MAXN], dp[MAXN], ans;
LL dp_sum[1 << 20], cntz[MAXN], pre[1 << 20];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> a[i];
    sum[i] = sum[i - 1] ^ a[i];
  }

  dp[0] = 1;
  for (int i = 1; i <= N; i++) {
    if (pre[sum[i]] == 0) {
      dp[i] = 1;
    } else {
      dp[i] = ((dp[pre[sum[i]]] - 1) +
               dp_sum[sum[i]] * (cntz[i - 1] - cntz[pre[sum[i]] - 1])) %
              MOD;
      //			cout << "Add " << dp_sum[sum[i]] * (cntz[i - 1]
      //- cntz[pre[sum[i]] - 1]) << ' ' << dp[pre[sum[i]]] - 1 << '\n'; 			cout <<
      //"Before " << i << " get " << dp[i] << '\n';
      if (sum[i] == 0)
        dp[i] = (dp[i] - dp[pre[sum[i]]]) % MOD;
      //			cout << "After " << i << " get " << dp[i] <<
      //'\n';
      dp[i] = (dp[i] + 1) % MOD;
      //			dp[i] = 1;
      //			for (int j = 1; j < i; j++) {
      //				if (sum[j] == sum[i]) {
      //					for (int k = j + 1; k < i; k++)
      //{ 						if (sum[k] == 0) { 							dp[i] = (dp[i] + dp[j]) % MOD;
      //						}
      //					}
      //				}
      //			}
    }

    pre[sum[i]] = i;
    cntz[i] = cntz[i - 1] + (sum[i] == 0);
    dp_sum[sum[i]] = (dp_sum[sum[i]] + dp[i]) % MOD;
    if (sum[N] == 0)
      ans += dp[i];
    //		cout << "At " << i << " get " << dp[i] << '\n';
  }

  if (sum[N] != 0)
    ans = dp[N];
  cout << (ans % MOD + MOD) % MOD << '\n';

  return 0;
}