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: Accepted)

#include <bits/stdc++.h>
using namespace std;
#define all(a) begin(a), end(a)
#define csz(a) (int) a.size()
#define pb push_back
#define epb emplace_back
#define mp make_pair
#define load(a, v) fill(begin(a), end(a), v)
#define load_mem(a, v) memset(a, v, sizeof(a));
#define iostream_optimize() ios::sync_with_stdio(false); cin.tie(0);
#define long long long
const long MOD = 1e9+7, LINF = 1e18 + 1e16;
const int INF = 1e9+1;
const double EPS = 1e-10;
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
/* <<<<<<<<<<<<<<<<<<<<<<<<<<<< END TEMPLATE >>>>>>>>>>>>>>>>>>>>>>>>>>> */
const int N = 2.5e5+1;
long P1, P2;
seed_seq seq{
	(uint64_t) chrono::duration_cast<chrono::nanoseconds>(chrono::high_resolution_clock::now().time_since_epoch()).count(),
  (uint64_t) __builtin_ia32_rdtsc(),
  (uint64_t) (uintptr_t) make_unique<char>().get()
};

long modPow(long b, long e) {
	if(!e) return 1;
	if(e == 1) return b;
	return modPow(b*b % MOD, e >> 1) * modPow(b, e & 1) % MOD;
}

int n;
char s[N];
long ans, shift[N], h1[N], h2[N];
map<pair<long,long>, int> cnt;

void gen_base() {
	mt19937 rng(seq);
	P1 = uniform_int_distribution<int>(n+1, MOD-1)(rng);
	P2 = uniform_int_distribution<int>(n+1, MOD-1)(rng);
}

int main() {
	iostream_optimize();
	cin >> n >> (s+1);
	gen_base();
	shift[0] = n;
	for(int i = 1; i <= n; i++) {
		shift[i] = shift[i-1];
		h1[i] = h1[i-1];
		h2[i] = h2[i-1];
		if(s[i] == '>') --shift[i];
		if(s[i] == '<') ++shift[i];
		if(s[i] == '+') {
			h1[i] = (h1[i] + modPow(P1, shift[i])) % MOD;
			h2[i] = (h2[i] + modPow(P2, shift[i])) % MOD;
		}
		if(s[i] == '-') {
			h1[i] = (h1[i] - modPow(P1, shift[i]) + MOD) % MOD;
			h2[i] = (h2[i] - modPow(P2, shift[i]) + MOD) % MOD;
		}
	}
	++cnt[make_pair(h1[n], h2[n])];
	for(int i = 1; i <= n; i++) {
		ans += cnt[make_pair(h1[i], h2[i])];
		long coeff1 = (n > shift[i] ? modPow(modPow(P1, n - shift[i]), MOD-2) : modPow(P1, shift[i] - n));
		long coeff2 = (n > shift[i] ? modPow(modPow(P2, n - shift[i]), MOD-2) : modPow(P2, shift[i] - n));
		// cout << shift[i] << ' ' << h[i] << ' ' << (h[n] * coeff + h[i]) % MOD <<  ' ' << cnt[h[i]] << endl;
		pair<long,long> add = {(h1[n] * coeff1 + h1[i]) % MOD, (h2[n] * coeff2 + h2[i]) % MOD};
		++cnt[add];
	}
	cout << ans;
}