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>
#define MN 250000
#define mod 1004535809
#define mod2 1000000007
using namespace std;
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
char s[MN+5];
map<int,int> mp,mp2;long long ans;
int n,pw[MN*2+5],invpw[MN*2+5],pw2[MN*2+5],invpw2[MN*2+5];
int Ha[MN+5],pos[MN+5],ha[MN+5];
inline int PW(int x){return x>=0?pw[x]:invpw[-x];}
inline int PW2(int x){return x>=0?pw2[x]:invpw2[-x];}
int main()
{
	n=read();scanf("%s",s+1);pw[0]=invpw[0]=pw2[0]=invpw2[0]=1;
	for(int i=1;i<=n*2;++i) 
		pw[i]=786433LL*pw[i-1]%mod,invpw[i]=317967460LL*invpw[i-1]%mod,
		pw2[i]=5767169LL*pw2[i-1]%mod2,invpw2[i]=170959791LL*invpw2[i-1]%mod2;
	for(int i=1;i<=n;++i)
	{
		if(s[i]=='<'||s[i]=='>') 
			pos[i]=pos[i-1]+(s[i]=='<'?-1:1),
			ha[i]=ha[i-1],Ha[i]=Ha[i-1];
		else pos[i]=pos[i-1],
			ha[i]=(ha[i-1]+1LL*(s[i]=='+'?1:mod-1)*pw[pos[i]+n])%mod,
			Ha[i]=(Ha[i-1]+1LL*(s[i]=='+'?1:mod2-1)*pw2[pos[i]+n])%mod2;
	}
	for(int i=n;i;--i)
	{
		++mp[(ha[n]-ha[i]+mod)%mod];++mp2[(Ha[n]-Ha[i]+mod2)%mod2];
		ans+=min(mp[(1LL*ha[n]*(mod+1-PW(pos[i-1]))+mod-ha[i-1])%mod],
			     mp2[(1LL*Ha[n]*(mod2+1-PW2(pos[i-1]))+mod2-Ha[i-1])%mod2]);
	}
	cout<<ans;
	return 0;
}