Problem p02962 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
// #include<cstdio>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define Would
#define you
#define please

const ll mask30 = (1ll << 30) - 1;
const ll mask31 = (1ll << 31) - 1;
const ll RHmod = (1ll << 61) - 1;
const ll len = 1000100;
ll POW[len + 1], RH[len + 1];
ll RHmul(ll A, ll B) {
  ll A1 = A >> 31;
  ll A2 = A & mask31;
  ll B1 = B >> 31;
  ll B2 = B & mask31;
  ll ret = 2 * A1 * B1 + A2 * B2 + ((A1 * B2 + A2 * B1 & mask30) << 31) +
           (A1 * B2 + A2 * B1 >> 30);
  return ret % RHmod;
}
ll RHcalc(int i, int j) {
  ll kari = (RH[j] - RH[i] + RHmod) % RHmod;
  return RHmul(kari, POW[len - i]);
}

int E[500001] = {};
int N;
int memo[500001];
int mugen;
int sagasu(int A, int B) {
  if (memo[A] >= 0)
    return memo[A];
  if (mugen)
    return 0;
  if (A == B) {
    mugen = 1;
    return 0;
  }
  if (B == -1)
    B = A;
  if (E[A] == -1)
    return memo[A] = 0;
  return memo[A] = sagasu(E[A], B) + 1;
}

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

  string S, T;
  cin >> S >> T;

  N = S.size();
  while (S.size() < max(N * 2, (int)T.size() * 2))
    S += S;
  int N2 = S.size();
  int M = T.size();

  POW[0] = 1;
  rep(i, len) POW[i + 1] = RHmul(POW[i], 30);
  RH[0] = 0;
  rep(i, N2) RH[i + 1] = (RH[i] + RHmul(POW[i], S[i] - 'a' + 1)) % RHmod;

  ll RHT = 0;
  rep(i, M) RHT = (RHT + RHmul(POW[i], T[i] - 'a' + 1)) % RHmod;
  RHT = RHmul(RHT, POW[len]);

  rep(i, N) {
    if (RHT == RHcalc(i, i + M))
      E[i] = (i + M) % N;
    else
      E[i] = -1;
  }

  rep(i, N) memo[i] = -1;

  int kotae = 0;
  rep(i, N) {
    kotae = max(kotae, sagasu(i, -1));
    if (mugen)
      break;
  }

  if (mugen)
    co("-1");
  else
    co(kotae);

  Would you please return 0;
}

Fixed submission:
#include <bits/stdc++.h>
// #include<cstdio>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define Would
#define you
#define please

const ll mask30 = (1ll << 30) - 1;
const ll mask31 = (1ll << 31) - 1;
const ll RHmod = (1ll << 61) - 1;
const ll len = 1000100;
ll POW[len + 1], RH[len + 1];
ll RHmul(ll A, ll B) {
  ll A1 = A >> 31;
  ll A2 = A & mask31;
  ll B1 = B >> 31;
  ll B2 = B & mask31;
  ll ret = 2 * A1 * B1 + A2 * B2 + ((A1 * B2 + A2 * B1 & mask30) << 31) +
           (A1 * B2 + A2 * B1 >> 30);
  return ret % RHmod;
}
ll RHcalc(int i, int j) {
  ll kari = (RH[j] - RH[i] + RHmod) % RHmod;
  return RHmul(kari, POW[len - i]);
}

int E[500001] = {};
int N;
int memo[500001];
int mugen;
int sagasu(int A, int B) {
  if (memo[A] >= 0)
    return memo[A];
  if (mugen)
    return 0;
  if (A == B) {
    mugen = 1;
    return 0;
  }
  if (B == -1)
    B = A;
  if (E[A] == -1)
    return memo[A] = 0;
  return memo[A] = sagasu(E[A], B) + 1;
}

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

  string S, T;
  cin >> S >> T;

  N = S.size();
  while (S.size() < max(N * 2, (int)T.size() * 2))
    S += S;
  int N2 = min(1000010, (int)S.size());
  int M = T.size();

  POW[0] = 1;
  rep(i, len) POW[i + 1] = RHmul(POW[i], 30);
  RH[0] = 0;
  rep(i, N2) RH[i + 1] = (RH[i] + RHmul(POW[i], S[i] - 'a' + 1)) % RHmod;

  ll RHT = 0;
  rep(i, M) RHT = (RHT + RHmul(POW[i], T[i] - 'a' + 1)) % RHmod;
  RHT = RHmul(RHT, POW[len]);

  rep(i, N) {
    if (RHT == RHcalc(i, i + M))
      E[i] = (i + M) % N;
    else
      E[i] = -1;
  }

  rep(i, N) memo[i] = -1;

  int kotae = 0;
  rep(i, N) {
    kotae = max(kotae, sagasu(i, -1));
    if (mugen)
      break;
  }

  if (mugen)
    co("-1");
  else
    co(kotae);

  Would you please return 0;
}