Problem p03322 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int MAXN = 2e5 + 5;
const int BASE = 3137;
const int MOD1 = 1e9 + 7;
const int MOD2 = 1e9 + 9;

int N;
char s[MAXN];
pii pref[MAXN], pot[MAXN];
map<pii, int> cnt;

void load() { scanf("%d%s", &N, s + 1); }

inline int add(int x, int y, int mod) {
  x += y;
  if (x >= mod)
    x -= mod;
  else if (x < 0)
    x += mod;
  return x;
}

inline int mul(int x, int y, int mod) { return (ll)x * y % mod; }

int power(int x, int y, int mod) {
  int res = 1;
  for (; y; y /= 2) {
    if (y % 2)
      res = mul(res, x, mod);
    x = mul(x, x, mod);
  }
  return res;
}

pii operator+(const pii &l, const pii &r) {
  return {add(l.first, r.first, MOD1), add(l.second, r.second, MOD2)};
}

pii operator*(const pii &l, const pii &r) {
  return {mul(l.first, r.first, MOD1), mul(l.second, r.second, MOD2)};
}

ll solve() {
  int inv1 = power(BASE, MOD1 - 2, MOD1);
  int inv2 = power(BASE, MOD2 - 2, MOD2);
  pot[0] = {1, 1};
  for (int i = 1; i <= N; i++) {
    pot[i] = pot[i - 1];
    pref[i] = pref[i - 1];
    if (s[i] == '>')
      pot[i] = pot[i] * pii(BASE, BASE);
    else if (s[i] == '<')
      pot[i] = pot[i] * pii(inv1, inv2);
    else if (s[i] == '+')
      pref[i] = pref[i] + pot[i];
    else
      pref[i] = pref[i] + pii(-pot[i].first, -pot[i].second);
  }

  ll ans = 0;
  for (int i = N; i; i--) {
    cnt[pref[i]]++;
    ans += cnt[(pref[N] * pot[i - 1]) + pref[i - 1]];
  }

  return ans;
}

int main() {
  load();
  printf("%lld\n", solve());
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int MAXN = 3e5 + 5;
const int BASE = 3137;
const int MOD1 = 1e9 + 7;
const int MOD2 = 1e9 + 9;

int N;
char s[MAXN];
pii pref[MAXN], pot[MAXN];
map<pii, int> cnt;

void load() { scanf("%d%s", &N, s + 1); }

inline int add(int x, int y, int mod) {
  x += y;
  if (x >= mod)
    x -= mod;
  else if (x < 0)
    x += mod;
  return x;
}

inline int mul(int x, int y, int mod) { return (ll)x * y % mod; }

int power(int x, int y, int mod) {
  int res = 1;
  for (; y; y /= 2) {
    if (y % 2)
      res = mul(res, x, mod);
    x = mul(x, x, mod);
  }
  return res;
}

pii operator+(const pii &l, const pii &r) {
  return {add(l.first, r.first, MOD1), add(l.second, r.second, MOD2)};
}

pii operator*(const pii &l, const pii &r) {
  return {mul(l.first, r.first, MOD1), mul(l.second, r.second, MOD2)};
}

ll solve() {
  int inv1 = power(BASE, MOD1 - 2, MOD1);
  int inv2 = power(BASE, MOD2 - 2, MOD2);
  pot[0] = {1, 1};
  for (int i = 1; i <= N; i++) {
    pot[i] = pot[i - 1];
    pref[i] = pref[i - 1];
    if (s[i] == '>')
      pot[i] = pot[i] * pii(BASE, BASE);
    else if (s[i] == '<')
      pot[i] = pot[i] * pii(inv1, inv2);
    else if (s[i] == '+')
      pref[i] = pref[i] + pot[i];
    else
      pref[i] = pref[i] + pii(-pot[i].first, -pot[i].second);
  }

  ll ans = 0;
  for (int i = N; i; i--) {
    cnt[pref[i]]++;
    ans += cnt[(pref[N] * pot[i - 1]) + pref[i - 1]];
  }

  return ans;
}

int main() {
  load();
  printf("%lld\n", solve());
  return 0;
}