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++ (GCC 9.2.1) (judge verdict: Wrong Answer)

#include<cstdio>
#include<map>
#define N 500001
#define mod 998244353
using namespace std;
long long thn[N],ans,qz[N],a[N];
char st[N];
int where[N];
int n;
map<long long,long long>maps;
long long ksmi(long long x,long long y)
{
	long long total=1,ab=x,p=y;
	while (p)
	{
		if (p%2==1) total=total*ab%mod;
		ab=ab*ab%mod;
		p/=2;
	}
	return total;
}
long long abs(long long x)
{
	if (x<0) return -x;
	return x;
}
int main()
{
	scanf("%d\n",&n);
	thn[0]=1;
	for (int i=1;i<=2*n;i++) thn[i]=thn[i-1]*(2*n)%mod;
	scanf("%s",st+1);
	int nowp=n;
	where[0]=n;
	qz[0]=0;
	for (int i=1;i<=2*n;i++)
		qz[0]=(qz[0]+thn[i-1]*n%mod)%mod;
	for (int i=1;i<=n;i++)
	{
		qz[i]=qz[i-1];
		if (st[i]=='+')
		{
			a[nowp]++;
			qz[i]=(qz[i]+thn[nowp-1])%mod;
		}
		if (st[i]=='-')
		{
			a[nowp]--;
			qz[i]=(qz[i]-thn[nowp-1]+mod)%mod;
		}
		if (st[i]=='>') nowp++;
		if (st[i]=='<') nowp--;
		where[i]=nowp;
	}
	unsigned long long a_hash=0;
	for (int i=1;i<=n*2;i++) a_hash=(a_hash+(a[i]+n)*thn[i-1])%mod;
	for (int i=n;i>=1;i--)
	{
		long long target=(a_hash-qz[0]+mod)%mod;
		if (where[i-1]<where[0])
			target=target*ksmi(thn[abs(where[i-1]-where[0])],mod-2)%mod;
		else	target=target*thn[abs(where[0]-where[i-1])]%mod;
		maps[qz[i]]++;
		target+=qz[i-1];
		target%=mod;
		ans=(ans+maps[target]);
	}
	printf("%lld\n",ans);
}