Problem p03322 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
#include <map>

using namespace std;

typedef long long LL;

#define mkp make_pair
#define pll pair<LL, LL>

const LL B1 = 237ll;
const LL B2 = 637ll;
const int N = 200010;
const LL P1 = 998244353;
const LL P2 = 1004535809;

int n;
LL ans;
char s[N];
LL I1, I2;
LL S[N], T[N];
map<pll, LL> buc;

LL expow(LL a, LL b, LL p) {
  LL ret = 1;
  while (b) {
    if (b & 1)
      (ret *= a) %= p;
    (a *= a) %= p;
    b >>= 1;
  }
  return ret;
}
int main() {
  cin >> n >> (s + 1);
  I1 = expow(B1, P1 - 2, P1);
  I2 = expow(B2, P2 - 2, P2);
  for (int i = n; i >= 1; --i) {
    if (s[i] == '-') {
      S[i] = (S[i + 1] - B1 + P1) % P1;
      T[i] = (T[i + 1] - B2 + P2) % P2;
    }
    if (s[i] == '+') {
      S[i] = (S[i + 1] + B1) % P1;
      T[i] = (T[i + 1] + B2) % P2;
    }
    if (s[i] == '<') {
      S[i] = (S[i + 1] * I1) % P1;
      T[i] = (T[i + 1] * I2) % P2;
    }
    if (s[i] == '>') {
      S[i] = (S[i + 1] * B1) % P1;
      T[i] = (T[i + 1] * B2) % P2;
    }
    // cout << S[i] << " " << T[i] << endl ;
  }
  LL gs = 1, gt = 1;
  for (int i = n; i >= 1; --i) {
    LL x, y;
    x = expow(gs, P1 - 2, P1) * (-S[i + 1] + P1) % P1;
    y = expow(gt, P2 - 2, P2) * (-T[i + 1] + P2) % P2;
    if (x < 0)
      x += P1;
    if (y < 0)
      y += P2;
    ++buc[mkp(x, y)];
    // cout << x << " " << y << endl ;
    if (s[i] == '<')
      (gs *= I1) %= P1, (gt *= I2) %= P2;
    if (s[i] == '>')
      (gs *= B1) %= P1, (gt *= B2) %= P2;
    x = expow(gs, P1 - 2, P1) * (-S[i] + S[1]) % P1;
    y = expow(gt, P2 - 2, P2) * (-T[i] + T[1]) % P2;
    if (x < 0)
      x += P1;
    if (y < 0)
      y += P2;
    ans += buc[mkp(x, y)];
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <cstdio>
#include <iostream>
#include <map>

using namespace std;

typedef long long LL;

#define mkp make_pair
#define pll pair<LL, LL>

const LL B1 = 237ll;
const LL B2 = 637ll;
const int N = 300010;
const LL P1 = 998244353;
const LL P2 = 1004535809;

int n;
LL ans;
char s[N];
LL I1, I2;
LL S[N], T[N];
map<pll, LL> buc;

LL expow(LL a, LL b, LL p) {
  LL ret = 1;
  while (b) {
    if (b & 1)
      (ret *= a) %= p;
    (a *= a) %= p;
    b >>= 1;
  }
  return ret;
}
int main() {
  cin >> n >> (s + 1);
  I1 = expow(B1, P1 - 2, P1);
  I2 = expow(B2, P2 - 2, P2);
  for (int i = n; i >= 1; --i) {
    if (s[i] == '-') {
      S[i] = (S[i + 1] - B1 + P1) % P1;
      T[i] = (T[i + 1] - B2 + P2) % P2;
    }
    if (s[i] == '+') {
      S[i] = (S[i + 1] + B1) % P1;
      T[i] = (T[i + 1] + B2) % P2;
    }
    if (s[i] == '<') {
      S[i] = (S[i + 1] * I1) % P1;
      T[i] = (T[i + 1] * I2) % P2;
    }
    if (s[i] == '>') {
      S[i] = (S[i + 1] * B1) % P1;
      T[i] = (T[i + 1] * B2) % P2;
    }
    // cout << S[i] << " " << T[i] << endl ;
  }
  LL gs = 1, gt = 1;
  for (int i = n; i >= 1; --i) {
    LL x, y;
    x = expow(gs, P1 - 2, P1) * (-S[i + 1] + P1) % P1;
    y = expow(gt, P2 - 2, P2) * (-T[i + 1] + P2) % P2;
    if (x < 0)
      x += P1;
    if (y < 0)
      y += P2;
    ++buc[mkp(x, y)];
    // cout << x << " " << y << endl ;
    if (s[i] == '<')
      (gs *= I1) %= P1, (gt *= I2) %= P2;
    if (s[i] == '>')
      (gs *= B1) %= P1, (gt *= B2) %= P2;
    x = expow(gs, P1 - 2, P1) * (-S[i] + S[1]) % P1;
    y = expow(gt, P2 - 2, P2) * (-T[i] + T[1]) % P2;
    if (x < 0)
      x += P1;
    if (y < 0)
      y += P2;
    ans += buc[mkp(x, y)];
  }
  cout << ans << endl;
  return 0;
}