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;
const int N = 5e5 + 10;
struct Tree {
  int l, r, mi, cnt, lazy_add;
} tree[N << 2];
void pushup(int pos) {
  tree[pos].mi = min(tree[pos << 1].mi, tree[pos << 1 | 1].mi);
  tree[pos].cnt = 0;
  if (tree[pos].mi == tree[pos << 1].mi) tree[pos].cnt += tree[pos << 1].cnt;
  if (tree[pos].mi == tree[pos << 1 | 1].mi)
    tree[pos].cnt += tree[pos << 1 | 1].cnt;
}
void pushdown(int pos) {
  if (!tree[pos].lazy_add) return;
  int val = tree[pos].lazy_add;
  tree[pos].lazy_add = 0;
  tree[pos << 1].mi += val;
  tree[pos << 1].lazy_add += val;
  tree[pos << 1 | 1].mi += val;
  tree[pos << 1 | 1].lazy_add += val;
}
void build(int pos, int l, int r) {
  tree[pos] = {l, r, 0, r - l + 1, 0};
  if (l == r) return;
  int mid = l + r >> 1;
  build(pos << 1, l, mid);
  build(pos << 1 | 1, mid + 1, r);
  pushup(pos);
}
void update(int pos, int l, int r, int val) {
  if (l <= tree[pos].l && tree[pos].r <= r) {
    tree[pos].mi += val;
    tree[pos].lazy_add += val;
    return;
  }
  pushdown(pos);
  int mid = tree[pos].l + tree[pos].r >> 1;
  if (l <= mid) update(pos << 1, l, r, val);
  if (mid < r) update(pos << 1 | 1, l, r, val);
  pushup(pos);
}
void query(int pos, int l, int r, int &mi, int &cnt) {
  if (l <= tree[pos].l && tree[pos].r <= r) {
    if (mi > tree[pos].mi) {
      mi = tree[pos].mi;
      cnt = tree[pos].cnt;
    } else if (mi == tree[pos].mi)
      cnt += tree[pos].cnt;
    return;
  }
  pushdown(pos);
  int mid = tree[pos].l + tree[pos].r >> 1;
  if (l <= mid) query(pos << 1, l, r, mi, cnt);
  if (mid < r) query(pos << 1 | 1, l, r, mi, cnt);
}
void debug(int pos) {
  if (tree[pos].l == tree[pos].r) {
    printf("debug:: a[%d] = (%d, %d)\n", tree[pos].l, tree[pos].mi,
           tree[pos].cnt);
    return;
  }
  pushdown(pos);
  debug(pos << 1);
  debug(pos << 1 | 1);
}
int main() {
  int n;
  scanf("%d", &n);
  vector<int> a(n + 1);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  build(1, 1, n);
  long long ans = 0;
  vector<vector<int>> last(n + 1, vector<int>(5, 0));
  for (int i = 1; i <= n; i++) {
    for (int j = 4; j >= 1; j--) last[a[i]][j] = last[a[i]][j - 1];
    last[a[i]][0] = i;
    update(1, last[a[i]][1] + 1, last[a[i]][0], 1);
    if (last[a[i]][2]) update(1, last[a[i]][3] + 1, last[a[i]][2], -1);
    if (last[a[i]][3]) update(1, last[a[i]][4] + 1, last[a[i]][3], 1);
    int mi = 1e9, cnt = 0;
    query(1, 1, i, mi, cnt);
    if (mi == 0) ans += cnt;
  }
  printf("%lld\n", ans);
  return 0;
}
