import sys from collections import defaultdict def main(): sys.setrecursionlimit(1 << 25) n, *rest = map(int, sys.stdin.read().split()) A = rest[:n] total = 0 freq = defaultdict(int) for num in A: freq[num] += 1 for x in freq: S = [0] * (n + 1) for i in range(1, n + 1): S[i] = S[i-1] + (1 if A[i-1] == x else -1) counts = defaultdict(int) counts[0] = 1 res = 0 for r in range(1, n + 1): current = S[r] left = current - 1 cnt = 0 while left >= -r: if left in counts: cnt += counts[left] left -= 1 res += cnt counts[current] = counts.get(current, 0) + 1 total += res print(total) if __name__ == '__main__': main()