You are given an array a consisting of n integers. We denote the subarray a[l..r] as the array [a_l, a_{l + 1}, ..., a_r] (1 ≤ l ≤ r ≤ n).

A subarray is considered good if every integer that occurs in this subarray occurs there exactly thrice. For example, the array [1, 2, 2, 2, 1, 1, 2, 2, 2] has three good subarrays:

  * a[1..6] = [1, 2, 2, 2, 1, 1]; 
  * a[2..4] = [2, 2, 2]; 
  * a[7..9] = [2, 2, 2]. 



Calculate the number of good subarrays of the given array a.

Input

The first line contains one integer n (1 ≤ n ≤ 5 ⋅ 10^5).

The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ n).

Output

Print one integer — the number of good subarrays of the array a.

Examples

Input


9
1 2 2 2 1 1 2 2 2


Output


3


Input


10
1 2 3 4 1 2 3 1 2 3


Output


0


Input


12
1 2 3 4 3 4 2 1 3 4 2 1


Output


1
#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma G++ optimize(2)
using namespace std;
std::mt19937_64 rnd(time(0));
const int MAX_N = 5e5 + 5;
inline int qi() {
  int f = 0, fu = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') fu = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    f = (f << 3) + (f << 1) + c - 48;
    c = getchar();
  }
  return f * fu;
}
inline long long ql() {
  long long f = 0;
  int fu = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') fu = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    f = (f << 3) + (f << 1) + c - 48;
    c = getchar();
  }
  return f * fu;
}
inline long long ceil_div(long long a, long long b) { return (a + b - 1) / b; }
queue<int> q[MAX_N];
int cnt[MAX_N];
unsigned long long base[MAX_N], hash_val[MAX_N];
unordered_map<unsigned long long, long long> mp;
int main() {
  int n = qi();
  for (register int i = 0, bbb = n; i <= bbb; ++i) base[i] = rnd();
  long long ans = 0;
  mp[hash_val[0] = 0]++;
  int st = 0;
  for (register int i = 1, bbb = n; i <= bbb; ++i) {
    int x = qi();
    if (q[x].size() >= 3) {
      int remove = q[x].front();
      q[x].pop();
      while (st < remove) mp[hash_val[st++]]--;
    }
    int last = cnt[x];
    cnt[x] = (cnt[x] + 1) % 3;
    hash_val[i] = hash_val[i - 1] + base[x] * (cnt[x] - last),
    ans += mp[hash_val[i]]++, q[x].push(i);
  }
  printf("%lld\n", ans);
  return 0;
}
