Problem p03322 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("tune=native")
#pragma GCC target("avx")
using ll = long long;
constexpr ll pow(const ll a, const ll b, const ll mod) {
  return b == 0       ? 1LL
         : b % 2 == 1 ? pow(a, b - 1, mod) * a % mod
                      : pow(a * a % mod, b / 2, mod);
}
constexpr ll inverse(const ll a, const ll mod) { return pow(a, mod - 2, mod); }
constexpr ll MOD0 = 1000000007LL, MOD1 = 1000000009LL, MOD2 = 1000000021;
constexpr ll X = 121234243;
constexpr ll XINV0 = inverse(X, MOD0), XINV1 = inverse(X, MOD1),
             XINV2 = inverse(X, MOD2);
constexpr int L = 250000;
using P = std::tuple<ll, ll, ll>;
P power[L] = {P{1, 1, 1}}, invpower[L] = {P{1, 1, 1}};
auto plus = [](const P &p1, const P &p2) {
  return P{(std::get<0>(p1) + std::get<0>(p2)) % MOD0,
           (std::get<1>(p1) + std::get<1>(p2)) % MOD1,
           (std::get<2>(p1) + std::get<2>(p2)) % MOD2};
};
auto minus = [](const P &p1, const P &p2) {
  return P{(std::get<0>(p1) + MOD0 - std::get<0>(p2)) % MOD0,
           (std::get<1>(p1) + MOD1 - std::get<1>(p2)) % MOD1,
           (std::get<2>(p1) + MOD2 - std::get<2>(p2)) % MOD2};
};
auto rshift = [](const P &p) {
  return P{std::get<0>(p) * X % MOD0, std::get<1>(p) * X % MOD1,
           std::get<2>(p) * X % MOD2};
};
auto lshift = [](const P &p) {
  return P{std::get<0>(p) * XINV0 % MOD0, std::get<1>(p) * XINV1 % MOD1,
           std::get<2>(p) * XINV2 % MOD2};
};
auto mul = [](const P &p1, const P &p2) {
  return P{std::get<0>(p1) * std::get<0>(p2) % MOD0,
           std::get<1>(p1) * std::get<1>(p2) % MOD1,
           std::get<2>(p1) * std::get<2>(p2) % MOD2};
};
int main() {
  for (int i = 1; i <= L; i++) {
    power[i] = rshift(power[i - 1]), invpower[i] = lshift(invpower[i - 1]);
  }
  auto invp = [](const int i) { return i >= 0 ? invpower[i] : power[-i]; };
  int N;
  std::string S;
  std::cin >> N >> S;
  std::vector<int> deg(N + 1, 0);
  std::vector<P> value(N + 1, P{0, 0, 0});
  for (int i = N - 1; i >= 0; i--) {
    if (S[i] == '-') {
      deg[i] = deg[i + 1], value[i] = minus(value[i + 1], P{1, 1, 1});
    } else if (S[i] == '+') {
      deg[i] = deg[i + 1], value[i] = plus(value[i + 1], P{1, 1, 1});
    } else if (S[i] == '>') {
      deg[i] = deg[i + 1] + 1, value[i] = rshift(value[i + 1]);
    } else {
      deg[i] = deg[i + 1] - 1, value[i] = lshift(value[i + 1]);
    }
  }
  const P answer = value[0];
  for (int i = 0; i <= N; i++) {
    value[i] = mul(minus(value[i], answer), invp(deg[i]));
  }
  std::map<P, ll> mp;
  ll ans = 0;
  for (int i = 0; i <= N; i++) {
    const P obj = plus(value[i], mul(invp(deg[i]), answer));
    if (mp.find(obj) != mp.end()) {
      ans += mp[obj];
    }
    mp[value[i]]++;
  }
  std::cout << ans << std::endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("tune=native")
#pragma GCC target("avx")
using ll = long long;
constexpr ll pow(const ll a, const ll b, const ll mod) {
  return b == 0       ? 1LL
         : b % 2 == 1 ? pow(a, b - 1, mod) * a % mod
                      : pow(a * a % mod, b / 2, mod);
}
constexpr ll inverse(const ll a, const ll mod) { return pow(a, mod - 2, mod); }
constexpr ll MOD0 = 1000000007LL, MOD1 = 1000000009LL, MOD2 = 1000000021;
constexpr ll X = 121234243;
constexpr ll XINV0 = inverse(X, MOD0), XINV1 = inverse(X, MOD1),
             XINV2 = inverse(X, MOD2);
constexpr int L = 250000;
using P = std::tuple<ll, ll, ll>;
P power[L + 1] = {P{1, 1, 1}}, invpower[L + 1] = {P{1, 1, 1}};
auto plus = [](const P &p1, const P &p2) {
  return P{(std::get<0>(p1) + std::get<0>(p2)) % MOD0,
           (std::get<1>(p1) + std::get<1>(p2)) % MOD1,
           (std::get<2>(p1) + std::get<2>(p2)) % MOD2};
};
auto minus = [](const P &p1, const P &p2) {
  return P{(std::get<0>(p1) + MOD0 - std::get<0>(p2)) % MOD0,
           (std::get<1>(p1) + MOD1 - std::get<1>(p2)) % MOD1,
           (std::get<2>(p1) + MOD2 - std::get<2>(p2)) % MOD2};
};
auto rshift = [](const P &p) {
  return P{std::get<0>(p) * X % MOD0, std::get<1>(p) * X % MOD1,
           std::get<2>(p) * X % MOD2};
};
auto lshift = [](const P &p) {
  return P{std::get<0>(p) * XINV0 % MOD0, std::get<1>(p) * XINV1 % MOD1,
           std::get<2>(p) * XINV2 % MOD2};
};
auto mul = [](const P &p1, const P &p2) {
  return P{std::get<0>(p1) * std::get<0>(p2) % MOD0,
           std::get<1>(p1) * std::get<1>(p2) % MOD1,
           std::get<2>(p1) * std::get<2>(p2) % MOD2};
};
int main() {
  for (int i = 1; i <= L; i++) {
    power[i] = rshift(power[i - 1]), invpower[i] = lshift(invpower[i - 1]);
  }
  auto invp = [](const int i) { return i >= 0 ? invpower[i] : power[-i]; };
  int N;
  std::string S;
  std::cin >> N >> S;
  std::vector<int> deg(N + 1, 0);
  std::vector<P> value(N + 1, P{0, 0, 0});
  for (int i = N - 1; i >= 0; i--) {
    if (S[i] == '-') {
      deg[i] = deg[i + 1], value[i] = minus(value[i + 1], P{1, 1, 1});
    } else if (S[i] == '+') {
      deg[i] = deg[i + 1], value[i] = plus(value[i + 1], P{1, 1, 1});
    } else if (S[i] == '>') {
      deg[i] = deg[i + 1] + 1, value[i] = rshift(value[i + 1]);
    } else {
      deg[i] = deg[i + 1] - 1, value[i] = lshift(value[i + 1]);
    }
  }
  const P answer = value[0];
  for (int i = 0; i <= N; i++) {
    value[i] = mul(minus(value[i], answer), invp(deg[i]));
  }
  std::map<P, ll> mp;
  ll ans = 0;
  for (int i = 0; i <= N; i++) {
    const P obj = plus(value[i], mul(invp(deg[i]), answer));
    if (mp.find(obj) != mp.end()) {
      ans += mp[obj];
    }
    mp[value[i]]++;
  }
  std::cout << ans << std::endl;
  return 0;
}