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<stdio.h>
    #include<vector>
    #include<algorithm>
    #include<string>
    #include<iostream>
    #include<map>
    using namespace std;
    typedef long long ll;
    ll m1 = 1000000007, m2 = 1000000009;
    ll t1 = 2, t2 = 1000000007;
    ll p1[505050], p2[505050];
    ll ip1[505050], ip2[505050];
    typedef pair<ll, ll>pii;
    ll po(ll a, ll b, ll m)
    {
    	if (b == 0)return 1;
    	ll z = po(a, b / 2, m);
    	z = z*z%m;
    	if (b & 1)z = z*a%m;
    	return z;
    }
    ll inv(ll a, ll m)
    {
    	return po(a, m - 2, m);
    }
    int main()
    {
    	int num;
    	scanf("%d", &num);
    	string s;
    	cin >> s;
    	p1[0] = p2[0] = ip1[0] = ip2[0] = 1;
    	for (int i = 0; i < 505000; i++)p1[i + 1] = p1[i] * t1%m1, p2[i + 1] = p2[i] * t2%m2;
    	ll it1 = inv(t1, m1), it2 = inv(t2, m2);
    	for (int i = 0; i < 505000; i++)ip1[i + 1] = ip1[i] * it1%m1, ip2[i + 1] = ip2[i] * it2%m2;
    	ll n1 = 0, n2 = 0;
    	vector<pii>v;
    	vector<int>pl;
    	v.push_back(make_pair(0, 0));
    	pl.push_back(num + 10);
    	int now = num + 10;
    	for (int i = 0; i < num; i++)
    	{
    		if (s[i] == '+')n1 = (n1 + p1[now]) % m1, n2 = (n2 + p2[now]) % m2;
    		else if (s[i] == '-')n1 = (n1 + m1 - p1[now]) % m1, n2 = (n2 + m2 - p2[now]) % m2;
    		else if (s[i] == '>')now++;
    		else now--;
    		v.push_back(make_pair(n1, n2)), pl.push_back(now);
    	}
    	ll ans = 0;
    	map<pii, int>ma;
    	for (int i = num; i >= 0; i--)
    	{
    		int dif = pl[i] - (num + 10);
    		ll mk1, mk2;
    		if (dif >= 0)mk1 = (v[i].first + n1*p1[dif]) % m1, mk2 = (v[i].second + n2*p2[dif]) % m2;
    		else mk1 = (v[i].first + n1*ip1[-dif]) % m1, mk2 = (v[i].second + n2*ip2[-dif]) % m2;
    		ans += ma[make_pair(mk1, mk2)];
    		ma[v[i]]++;
    	}
    	printf("%lld\n", ans);
    }