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 <bits/stdc++.h>

#define pb push_back
#define all(v) v.begin(), v.end()
#define sz(v) int(v.size())
#define pii pair<int, int>
#define mp make_pair
#define f first
#define ll long long
#define s second
#define vec vector<int>

using namespace std;

const int N = (int) 2e6 + 10;
const int K = (int) 1e7 + 10;
const int mod = (int) 1e9 + 7;
const int T = (int) 200017;

int n;
string st;
int p[N];

void add(int &x, int y) {
	x += y;
	if (x >= mod)
		x -= mod;
	if (x < 0) 
		x += mod;
}

int mult(int x, int y) {
	return x * 1ll * y % mod;
}

int hsh[N], pos[N];

pair<int, int> get(int l, int r) {
	return mp(hsh[r + 1], pos[r + 1]);
}

int inv(int x) {
	int pw = mod - 2;
	int ans = 1;
	while (pw) {
		if (pw & 1) 
			ans = mult(ans, x);
		x = mult(x, x);
		pw /= 2;
	}
	return ans;
}

// get(l,r)=(get(0,r)-get(0,l-1))<<pos(0, l-1)

int shift(int x, int val) { // to the right
	if (val > 0) {
		x = mult(x, p[val]);
	} else {
		x = mult(x, inv(p[-val]));
	}
	return x;
}

int sum(int x, int y) {
	add(x, y);
	return x;
}

int main() {
	#ifdef sony
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	#endif
	cin >> n >> st;
	//HASH(A) = {FOR I = [0...big_N] SUM OF A[I]*T^I % MODULO} WHERE T = PRIME > 2.5 * 10^5
	p[0] = 1;
	for (int i = 1; i < N; i++) {
		p[i] = mult(p[i - 1], T);
	}
	pos[0] = 2 * n;
	hsh[0] = 0;
	vector<int> a(4 * n, 0);
	for (int i = 0; i < n; i++) {
		pos[i + 1] = pos[i];
		hsh[i + 1] = hsh[i];
		if (st[i] == '+') {
			add(hsh[i + 1], p[pos[i + 1]]);
		} else if (st[i] == '-') {
			add(hsh[i + 1], -p[pos[i + 1]]);
		} else if (st[i] == '>') {
			pos[i + 1]++;
		} else {
			pos[i + 1]--;
		}
	}
	int x = get(0, n - 1).f;
	int ans = 0;
	map<int, int> cnt;
	for (int i = 0; i < n; i++) {
		int y = sum(shift(x, get(0, i - 1).s - 2 * n), shift(get(0, i - 1).f, 0));
		cnt[y]++;
		int z = shift(get(0, i).f, 0);
		ans += cnt[z];
	}
	cout << ans;
	return 0;
}