Problem p02816 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define mod 1000000007
#define fi first
#define sc second
#define rep(i, x) for (int i = 0; i < x; i++)
#define repn(i, x) for (int i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())
int n, a[200005], b[200005];
int c[200005], d[200005];
ll m[2][400005];
ll h[2][400005];
ll calc(int aa, int bb) { return c[aa] ^ d[bb]; }
int main() {
  cin >> n;
  rep(i, n) cin >> c[i];
  rep(i, n) cin >> d[i];

  rep(i, n) a[i] = (c[(i + 1) % n] ^ c[i]);
  rep(i, n) b[i] = (d[(i + 1) % n] ^ d[i]);

  rep(i, n) a[n + i] = a[i];

  m[0][0] = m[1][0] = 1;
  repn(i, 400003) {
    m[0][i] = m[0][i - 1] * 37LL % mod;
    m[1][i] = m[1][i - 1] * 41LL % mod;
  }
  rep(i, 2 * n) {
    h[0][i] = m[0][i] * a[i];
    h[1][i] = m[1][i] * a[i];
    if (i)
      h[0][i] += h[0][i - 1];
    if (i)
      h[1][i] += h[1][i - 1];
    h[0][i] %= mod;
    h[1][i] %= mod;
  }
  ll sum[2] = {};
  rep(i, n) rep(j, 2) { sum[j] = (sum[j] + m[j][i] * b[i]) % mod; }
  rep(k, n) {
    ll a = h[0][k + n - 1];
    if (k)
      a -= h[0][k - 1];
    ll b = h[1][k + n - 1];
    if (k)
      b -= h[1][k - 1];
    a = (a % mod + mod) % mod;
    b = (b % mod + mod) % mod;

    ll c = sum[0];
    ll d = sum[1];
    c = (c * m[0][k] % mod + mod) % mod;
    d = (d * m[1][k] % mod + mod) % mod;
    if (a == c && b == d) {
      cout << k << " " << calc(k, 0) << '\n';
    }
  }
}

Fixed submission:
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define mod 1000000007
#define fi first
#define sc second
#define rep(i, x) for (int i = 0; i < x; i++)
#define repn(i, x) for (int i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())
int n, a[400005], b[400005];
int c[400005], d[400005];
ll m[2][400005];
ll h[2][400005];
ll calc(int aa, int bb) { return c[aa] ^ d[bb]; }
int main() {
  cin >> n;
  rep(i, n) cin >> c[i];
  rep(i, n) cin >> d[i];

  rep(i, n) a[i] = (c[(i + 1) % n] ^ c[i]);
  rep(i, n) b[i] = (d[(i + 1) % n] ^ d[i]);

  rep(i, n) a[n + i] = a[i];

  m[0][0] = m[1][0] = 1;
  repn(i, 400003) {
    m[0][i] = m[0][i - 1] * 37LL % mod;
    m[1][i] = m[1][i - 1] * 41LL % mod;
  }
  rep(i, 2 * n) {
    h[0][i] = m[0][i] * a[i];
    h[1][i] = m[1][i] * a[i];
    if (i)
      h[0][i] += h[0][i - 1];
    if (i)
      h[1][i] += h[1][i - 1];
    h[0][i] %= mod;
    h[1][i] %= mod;
  }
  ll sum[2] = {};
  rep(i, n) rep(j, 2) { sum[j] = (sum[j] + m[j][i] * b[i]) % mod; }
  rep(k, n) {
    ll a = h[0][k + n - 1];
    if (k)
      a -= h[0][k - 1];
    ll b = h[1][k + n - 1];
    if (k)
      b -= h[1][k - 1];
    a = (a % mod + mod) % mod;
    b = (b % mod + mod) % mod;

    ll c = sum[0];
    ll d = sum[1];
    c = (c * m[0][k] % mod + mod) % mod;
    d = (d * m[1][k] % mod + mod) % mod;
    if (a == c && b == d) {
      cout << k << " " << calc(k, 0) << '\n';
    }
  }
}