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>
using namespace std;
int n, a[500015], pos[500015];
vector<int> o[500015];
namespace seg {
int lazy[500015 << 2], mx[500015 << 2], cnt[500015 << 2];
void build(int p, int l, int r) {
  mx[p] = 0;
  cnt[p] = r - l + 1;
  if (l == r) return;
  int mid = (l + r) >> 1;
  build((p << 1), l, mid);
  build((p << 1 | 1), mid + 1, r);
}
void change(int p, int l, int r, int x, int y, int v) {
  if (x <= l && r <= y) {
    lazy[p] += v;
    mx[p] += v;
    return;
  }
  int mid = (l + r) >> 1;
  if (x <= mid) change((p << 1), l, mid, x, y, v);
  if (y > mid) change((p << 1 | 1), mid + 1, r, x, y, v);
  mx[p] = max(mx[(p << 1)], mx[(p << 1 | 1)]);
  cnt[p] = 0;
  if (mx[p] == mx[(p << 1)]) cnt[p] += cnt[(p << 1)];
  if (mx[p] == mx[(p << 1 | 1)]) cnt[p] += cnt[(p << 1 | 1)];
  mx[p] += lazy[p];
}
}  // namespace seg
using namespace seg;
void change(int x, int v) {
  change(1, 0, n, o[x][pos[x]], o[x][pos[x] + 1] - 1, v);
  if (pos[x] + 3 < o[x].size() - 1)
    change(1, 0, n, o[x][pos[x] + 3], o[x][pos[x] + 4] - 1, v);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++) o[i].push_back(0);
  for (int i = 1; i <= n; i++) o[a[i]].push_back(i);
  for (int i = 1; i <= n; i++) o[i].push_back(n + 1);
  build(1, 0, n);
  for (int i = 1; i <= n; i++) change(i, 1);
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    change(1, 0, n, i - 1, i - 1, -0x3f3f3f3f);
    if (mx[1] == n) ans += cnt[1];
    change(a[i], -1);
    pos[a[i]]++;
    change(a[i], 1);
  }
  printf("%lld\n", ans);
  return 0;
}
