Problem p03322 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define dbg1(x) cerr << #x << "=" << (x) << " "
#define dbg2(x) cerr << #x << "=" << (x) << "\n"
#define dbg3(x) cerr << #x << "\n"
using namespace std;
#define reg register
const int MN = 2e5 + 5;
int N;
char s[MN];
const int P = 998244353;
int Add(int x, int y) { return (x + y) % P; }
int Mul(int x, int y) { return (1ll * x * y) % P; }
const int B[] = {691, 769, 823, 877, 911, 967},
          inv[] = {37560569,  983965175, 992179685,
                   349442436, 115055606, 356147158};
struct node {
  int v[6];
  int &operator[](int x) { return v[x]; }
  bool operator<(const node o) const {
    for (int i = 0; i < 6; ++i)
      if (v[i] != o.v[i])
        return v[i] < o.v[i];
    return false;
  }
  bool operator==(const node o) const {
    for (int i = 0; i < 6; ++i)
      if (v[i] != o.v[i])
        return false;
    return true;
  }
} pre[MN], all;
std::map<node, int> Mp;
int pos[MN], nm[6][MN << 1], cal[MN];
node get(node x, node y, int p) {
  for (int j = 0; j < 6; ++j)
    x[j] = Add(x[j], Mul(y[j], nm[j][p]));
  return x;
}
int main() {
  scanf("%d%s", &N, s + 1);
  reg int i, j, k;
  for (j = 0; j < 6; ++j)
    for (nm[j][MN] = i = 1; i <= N; ++i)
      nm[j][i + MN] = Mul(nm[j][i - 1 + MN], B[j]),
                nm[j][-i + MN] = Mul(nm[j][-i + 1 + MN], inv[j]);
  for (k = j = 0; j < 6; ++j, k = 0)
    for (i = 1; i <= N; ++i) {
      switch (s[i]) {
      case '<':
        --k;
        break;
      case '>':
        ++k;
        break;
      case '+':
        all[j] = Add(all[j], nm[j][k + MN]);
        break;
      case '-':
        all[j] = Add(all[j], P - nm[j][k + MN]);
        break;
      }
      pos[i] = k;
      pre[i][j] = all[j];
    }
  ++Mp[all];
  for (i = 1; i <= N; ++i)
    cal[i] = Mp[pre[i]], ++Mp[get(pre[i], all, pos[i] + MN)];
  ll ans = 0;
  for (i = 1; i <= N; ++i)
    ans += cal[i];
  return 0 * printf("%lld\n", ans);
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define dbg1(x) cerr << #x << "=" << (x) << " "
#define dbg2(x) cerr << #x << "=" << (x) << "\n"
#define dbg3(x) cerr << #x << "\n"
using namespace std;
#define reg register
const int MN = 250005;
int N;
char s[MN];
const int P = 998244353;
int Add(int x, int y) { return (x + y) % P; }
int Mul(int x, int y) { return (1ll * x * y) % P; }
const int B[] = {691, 769, 823, 877, 911, 967},
          inv[] = {37560569,  983965175, 992179685,
                   349442436, 115055606, 356147158};
struct node {
  int v[6];
  int &operator[](int x) { return v[x]; }
  bool operator<(const node o) const {
    for (int i = 0; i < 6; ++i)
      if (v[i] != o.v[i])
        return v[i] < o.v[i];
    return false;
  }
  bool operator==(const node o) const {
    for (int i = 0; i < 6; ++i)
      if (v[i] != o.v[i])
        return false;
    return true;
  }
} pre[MN], all;
std::map<node, int> Mp;
int pos[MN], nm[6][MN << 1], cal[MN];
node get(node x, node y, int p) {
  for (int j = 0; j < 6; ++j)
    x[j] = Add(x[j], Mul(y[j], nm[j][p]));
  return x;
}
int main() {
  scanf("%d%s", &N, s + 1);
  reg int i, j, k;
  for (j = 0; j < 6; ++j)
    for (nm[j][MN] = i = 1; i <= N; ++i)
      nm[j][i + MN] = Mul(nm[j][i - 1 + MN], B[j]),
                nm[j][-i + MN] = Mul(nm[j][-i + 1 + MN], inv[j]);
  for (k = j = 0; j < 6; ++j, k = 0)
    for (i = 1; i <= N; ++i) {
      switch (s[i]) {
      case '<':
        --k;
        break;
      case '>':
        ++k;
        break;
      case '+':
        all[j] = Add(all[j], nm[j][k + MN]);
        break;
      case '-':
        all[j] = Add(all[j], P - nm[j][k + MN]);
        break;
      }
      pos[i] = k;
      pre[i][j] = all[j];
    }
  ++Mp[all];
  for (i = 1; i <= N; ++i)
    cal[i] = Mp[pre[i]], ++Mp[get(pre[i], all, pos[i] + MN)];
  ll ans = 0;
  for (i = 1; i <= N; ++i)
    ans += cal[i];
  return 0 * printf("%lld\n", ans);
}