Problem p03322 (C++)

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcountll

#define INF 1e16
#define mod 1000000007

ll mod_pow(ll a, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

ll N;
string S;
const ll K = 6;
ll A[6][100010], B[6][100010];
vector<ll> C[100010];
map<vector<ll>, ll> cnt;

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

  vector<ll> xs{452, 8112, 5734, 56343, 8766754, 64537};

  cin >> N >> S;
  ll res = 0;
  rep(k, K) A[k][N] = 1;
  rep(k, K) C[N].push_back(0);
  for (ll j = N - 1; j >= 0; j--) {
    rep(i, K) {
      if (S[j] == '+') {
        A[i][j] = A[i][j + 1];
        B[i][j] = (B[i][j + 1] + 1) % mod;
      } else if (S[j] == '-') {
        A[i][j] = A[i][j + 1];
        B[i][j] = (B[i][j + 1] + mod - 1) % mod;
      } else if (S[j] == '>') {
        A[i][j] = A[i][j + 1] * mod_pow(xs[i], mod - 2) % mod;
        B[i][j] = B[i][j + 1] * mod_pow(xs[i], mod - 2) % mod;
      } else if (S[j] == '<') {
        A[i][j] = A[i][j + 1] * xs[i] % mod;
        B[i][j] = B[i][j + 1] * xs[i] % mod;
      }
      C[j].push_back(B[i][j] * mod_pow(A[i][j], mod - 2) % mod);
    }
  }

  for (ll i = N; i >= 0; i--) {
    vector<ll> nc;
    rep(k, K) {
      nc.push_back(((B[k][i] - B[k][0] + mod) % mod) *
                   mod_pow(A[k][i], mod - 2) % mod);
    }

    res += cnt[nc];
    cnt[C[i]]++;
  }

  cout << res << endl;

  return 0;
}


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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcountll

#define INF 1e16
#define mod 1000000007

ll mod_pow(ll a, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

ll N;
string S;
const ll K = 6;
ll A[6][250010], B[6][250010];
vector<ll> C[250010];
map<vector<ll>, ll> cnt;

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

  vector<ll> xs{452, 8112, 5734, 56343, 8766754, 64537};

  cin >> N >> S;
  ll res = 0;
  rep(k, K) A[k][N] = 1;
  rep(k, K) C[N].push_back(0);
  for (ll j = N - 1; j >= 0; j--) {
    rep(i, K) {
      if (S[j] == '+') {
        A[i][j] = A[i][j + 1];
        B[i][j] = (B[i][j + 1] + 1) % mod;
      } else if (S[j] == '-') {
        A[i][j] = A[i][j + 1];
        B[i][j] = (B[i][j + 1] + mod - 1) % mod;
      } else if (S[j] == '>') {
        A[i][j] = A[i][j + 1] * mod_pow(xs[i], mod - 2) % mod;
        B[i][j] = B[i][j + 1] * mod_pow(xs[i], mod - 2) % mod;
      } else if (S[j] == '<') {
        A[i][j] = A[i][j + 1] * xs[i] % mod;
        B[i][j] = B[i][j + 1] * xs[i] % mod;
      }
      C[j].push_back(B[i][j] * mod_pow(A[i][j], mod - 2) % mod);
    }
  }

  for (ll i = N; i >= 0; i--) {
    vector<ll> nc;
    rep(k, K) {
      nc.push_back(((B[k][i] - B[k][0] + mod) % mod) *
                   mod_pow(A[k][i], mod - 2) % mod);
    }

    res += cnt[nc];
    cnt[C[i]]++;
  }

  cout << res << endl;

  return 0;
}