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)

//#define __USE_MINGW_ANSI_STDIO 0
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
 
#define debug(x) cout<<#x<<" :: "<<x<<endl;
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<endl;
#define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<endl;
 
#define boost ios::sync_with_stdio(0); cin.tie(0)
 
#define fi first
#define se second
 
typedef long long ll;
typedef double ld;
typedef pair<int, int> pii;
 
const int N = 1e6 + 5;
const int MOD1 = 1934559071;
const int MOD2 = 1544907697;
const int B = 257707;
 
/***************************************************************************/
 
ll power(ll a, ll n, ll m) {
	if(n == 0) return 1;
	ll p = power(a, n>>1, m);
	p = (p * p) % m;
	if(n & 1) p = (p * a) % m;
	return p;
}
 
string S;
pii powB[N];
 
inline pii& pow(int n) {
	if(n < 0) n += N;
	return powB[n];
}
 
map<ll, int> M;
 
int main() {
 
	boost;
	int N; cin>>N;
	cin>>S;
 
	pow(-B) = pii(power(power(B, B, MOD1), MOD1-2, MOD1), power(power(B, B, MOD2), MOD2-2, MOD2));
	for(int i=-B+1; i<=B; i++) {
		pow(i) = pii((B * 1LL * pow(i-1).fi) % MOD1, (B * 1LL * pow(i-1).se) % MOD2);
	}
 
	ll phas1, phas2, chas1, chas2;
	phas1 = phas2 = chas1 = chas2 = 0;
 
	int p = 0;
	for(auto it : S) {
		if(it == '+') phas1 += pow(p).fi, phas2 += pow(p).se;
		else if(it == '-') phas1 -= pow(p).fi, phas2 -= pow(p).se;
		else if(it == '>') p++;
		else p--;
	}
 
	phas1 %= MOD1; if(phas1 < 0) phas1 += MOD1;
	phas2 %= MOD2; if(phas2 < 0) phas2 += MOD2;
 
	ll ans = 0;
	M[(phas1 << 32) | phas2]++;
    p = 0;
 
	for(auto it : S) {
		if(it == '+') chas1 = (chas1 + pow(p).fi) % MOD1, chas2 = (chas2 + pow(p).se) % MOD2;
		else if(it == '-') chas1 = (chas1 - pow(p).fi + MOD1) % MOD1, chas2 = (chas2 - pow(p).se + MOD2) % MOD2;
		else if(it == '>') p++;
		else p--;
 
		ans += M[(chas1 << 32) | chas2];
		
		ll has1 = (chas1 + pow(p).fi * phas1) % MOD1;
		ll has2 = (chas2 + pow(p).se * phas2) % MOD2;
 
		M[(has1 << 32) | has2]++;
	}
 
	cout<<ans<<"\n";
 
	return 0;
}