Problem:
Score : 1200 points

Problem Statement

In Takahashi's mind, there is always an integer sequence of length 2 \times 10^9 + 1: A = (A_{-10^9}, A_{-10^9 + 1}, ..., A_{10^9 - 1}, A_{10^9}) and an integer P.

Initially, all the elements in the sequence A in Takahashi's mind are 0, and the value of the integer P is 0.

When Takahashi eats symbols +, -, > and <, the sequence A and the integer P will change as follows:

When he eats +, the value of A_P increases by 1;

When he eats -, the value of A_P decreases by 1;

When he eats >, the value of P increases by 1;

When he eats <, the value of P decreases by 1.

Takahashi has a string S of length N. Each character in S is one of the symbols +, -, > and <.
He chose a pair of integers (i, j) such that 1 \leq i \leq j \leq N and ate the symbols that are the i-th, (i+1)-th, ..., j-th characters in S, in this order.
We heard that, after he finished eating, the sequence A became the same as if he had eaten all the symbols in S from first to last.
How many such possible pairs (i, j) are there?

Constraints

1 \leq N \leq 250000

|S| = N

Each character in S is +, -, > or <.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the answer.

Sample Input 1

5
+>+<-

Sample Output 1

3

If Takahashi eats all the symbols in S, A_1 = 1 and all other elements would be 0.
The pairs (i, j) that leads to the same sequence A are as follows:

(1, 5)

(2, 3)

(2, 4)

Sample Input 2

5
+>+-<

Sample Output 2

5

Note that the value of P may be different from the value when Takahashi eats all the symbols in S.

Sample Input 3

48
-+><<><><><>>>+-<<>->>><<><<-+<>><+<<>+><-+->><<

Sample Output 3

475

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;
  
#define ll long long
#define all(aaa) aaa.begin(), aaa.end()

const int MOD = 1e9 + 9, N = 3e5 + 5;
const ll X = 1234567;

ll bp(ll a, ll p) {
    ll r = 1;
    while (p > 0) {
        if (p & 1) {
            r = r * a % MOD;
        }
        a = a * a % MOD;
        p >>= 1;
    }
    return r;
}

ll inv_X = bp(X, MOD - 2);

ll a[N], b[N];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n;
    string s;
    cin >> n >> s;

    ll h = 0;

    for (int i = n - 1; i >= 0; i--) {
        if (s[i] == '>')
            h = h * X % MOD;
        else if (s[i] == '<')
            h = h * inv_X % MOD;
        else if (s[i] == '+')
            h = (h + 1) % MOD;
        else
            h = (h - 1 + MOD) % MOD;
    }

    map<ll, int> mp;

    ll ans = 0;

    mp[0]++;

    a[n] = 1;

    for (int i = n - 1; i >= 0; i--) {
        a[i] = a[i + 1];
        b[i] = b[i + 1];

        if (s[i] == '>') {
            a[i] = a[i + 1] * inv_X % MOD;
            b[i] = b[i + 1];
        }
        else if (s[i] == '<') {
            a[i] = a[i + 1] * X % MOD;
            b[i] = b[i + 1];
        }
        else if (s[i] == '+') {
            a[i] = a[i + 1];
            b[i] = (b[i + 1] - a[i + 1] + MOD) % MOD;
        }
        else {
            a[i] = a[i + 1];
            b[i] = (b[i + 1] + a[i + 1]) % MOD;
        }

        // cout << b[i] << "\n";

        ans += mp[(a[i] * h + b[i]) % MOD];
        mp[b[i]]++;
    }

    // cout << (a[0] * h + b[0]) % MOD << "\n";

    cout << ans << "\n";

    return 0;   
}