def main(): import sys input = sys.stdin.read().split() n = int(input[0]) A = list(map(int, input[1:n+1])) present = {} for num in A: present[num] = True max_consecutive = 0 current = 1 while current in present: max_consecutive += 1 current += 1 def count_ge(k): if k == 1: return n * (n + 1) // 2 m = k - 1 if m > max_consecutive: return 0 required = set(range(1, m + 1)) freq = {} left = 0 cnt = 0 res = 0 for right in range(n): num = A[right] if num in required: if num not in freq or freq[num] == 0: cnt += 1 freq[num] = freq.get(num, 0) + 1 while cnt == len(required): res += n - right num_left = A[left] if num_left in required: freq[num_left] -= 1 if freq[num_left] == 0: cnt -= 1 left += 1 return res total = 0 max_k = max_consecutive + 1 for k in range(1, max_k + 1): total += count_ge(k) print(total) if __name__ == '__main__': main()