Problem p02589 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end()
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long long ll;

template <class T> void read(T &x) {
  int f = 0;
  x = 0;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    f |= (ch == '-');
  for (; isdigit(ch); ch = getchar())
    x = x * 10 + ch - '0';
  if (f)
    x = -x;
}

const int N = 1000005;
const int mod[2] = {1000000009, 1019260817};
const int seed[2] = {666, 19260817};
namespace hsh {
const int M = 3051439;
int A[M], B[M], L[M], h[M], sz;
int harsh(ll a, ll b) {
  return ((a << 13) ^ (a >> 2) ^ (a << 4) ^ (b << 3) ^ (b >> 5)) % M;
}
void ins(int a, int b) {
  int ha = harsh(a, b);
  A[++sz] = a, B[sz] = b, L[sz] = h[ha], h[ha] = sz;
}
int qry(int a, int b) {
  int k = h[harsh(a, b)];
  for (; k; k = L[k]) {
    if (A[k] == a && B[k] == b) {
      return 1;
    }
  }
  return 0;
}
} // namespace hsh
int f[2][N], rk[N], flag[26], n;
unordered_map<ll, int> cnt;
string s[N];
ll ans;

int main() {
  read(n);
  ios::sync_with_stdio(false);
  rep(i, 1, n) {
    cin >> s[i];
    rk[i] = i;
  }
  sort(rk + 1, rk + n + 1,
       [&](int a, int b) { return s[a].size() < s[b].size(); });
  rep(i, 1, n) {
    int k = rk[i], m = s[k].size();
    REP(t, 2) per(j, m, 1) {
      f[t][j] = ((ll)f[t][j + 1] * seed[t] + s[k][j - 1] - 'a' + 1) % mod[t];
    }
    memset(flag, 0, sizeof flag);
    rep(j, 1, m) {
      flag[s[k][j - 1] - 'a']++;
      REP(c, 26) if (flag[c]) {
        int A = ((ll)f[0][j + 1] * seed[0] + c + 1) % mod[0];
        int B = ((ll)f[1][j + 1] * seed[1] + c + 1) % mod[1];
        ans += hsh::qry(A, B);
      }
    }
    hsh::ins(f[0][1], f[1][1]);
  }
  cout << ans << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end()
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long long ll;

template <class T> void read(T &x) {
  int f = 0;
  x = 0;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    f |= (ch == '-');
  for (; isdigit(ch); ch = getchar())
    x = x * 10 + ch - '0';
  if (f)
    x = -x;
}

const int N = 1000005;
const int mod[2] = {1000000009, 1019260817};
const int seed[2] = {666, 19260817};
namespace hsh {
const int M = 3051439;
int A[M], B[M], L[M], h[M], sz;
int harsh(int a, int b) {
  return ((a << 13) ^ (a >> 2) ^ (a << 4) ^ (b << 3) ^ (b >> 5)) % M;
}
void ins(int a, int b) {
  int ha = harsh(a, b);
  A[++sz] = a, B[sz] = b, L[sz] = h[ha], h[ha] = sz;
}
int qry(int a, int b) {
  int k = h[harsh(a, b)];
  for (; k; k = L[k]) {
    if (A[k] == a && B[k] == b) {
      return 1;
    }
  }
  return 0;
}
} // namespace hsh
int f[2][N], rk[N], flag[26], n;
unordered_map<ll, int> cnt;
string s[N];
ll ans;

int main() {
  read(n);
  ios::sync_with_stdio(false);
  rep(i, 1, n) {
    cin >> s[i];
    rk[i] = i;
  }
  sort(rk + 1, rk + n + 1,
       [&](int a, int b) { return s[a].size() < s[b].size(); });
  rep(i, 1, n) {
    int k = rk[i], m = s[k].size();
    REP(t, 2) per(j, m, 1) {
      f[t][j] = ((ll)f[t][j + 1] * seed[t] + s[k][j - 1] - 'a') % mod[t];
    }
    memset(flag, 0, sizeof flag);
    rep(j, 1, m) {
      flag[s[k][j - 1] - 'a']++;
      REP(c, 26) if (flag[c]) {
        int A = ((ll)f[0][j + 1] * seed[0] + c) % mod[0];
        int B = ((ll)f[1][j + 1] * seed[1] + c) % mod[1];
        ans += hsh::qry(A, B);
      }
    }
    hsh::ins(f[0][1], f[1][1]);
  }
  cout << ans << endl;
  return 0;
}