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 Maxn = 5e5 + 10;
int n;
long long ans;
vector<int> V[Maxn];
int read() {
  int ret = 0;
  char c = getchar();
  while (c < '0' || c > '9') c = getchar();
  while (c >= '0' && c <= '9') {
    ret = ret * 10 + c - '0';
    c = getchar();
  }
  return ret;
}
namespace Seg {
int mx[Maxn << 2], num[Maxn << 2], tag[Maxn << 2];
void up(int u) {
  int x = u << 1, y = (u << 1) | 1;
  if (mx[x] < mx[y]) swap(x, y);
  mx[u] = mx[x];
  if (mx[x] == mx[y])
    num[u] = num[x] + num[y];
  else
    num[u] = num[x];
}
void put_tag(int u, int key) {
  mx[u] += key;
  tag[u] += key;
}
void down(int u) {
  if (tag[u]) {
    put_tag(u << 1, tag[u]);
    put_tag((u << 1) | 1, tag[u]);
    tag[u] = 0;
  }
}
void build(int u, int l, int r) {
  num[u] = r - l + 1;
  if (l == r) return;
  int mid = (l + r) >> 1;
  build(u << 1, l, mid);
  build((u << 1) | 1, mid + 1, r);
}
void update(int u, int l, int r, int li, int ri, int key) {
  if (l > r || li > r || l > ri || li > ri) return;
  if (l == li && r == ri) {
    put_tag(u, key);
    return;
  }
  int mid = (l + r) >> 1;
  down(u);
  update(u << 1, l, mid, li, min(mid, ri), key);
  update((u << 1) | 1, mid + 1, r, max(mid + 1, li), ri, key);
  up(u);
}
int query(int u, int l, int r, int li, int ri) {
  if (l > r || li > r || l > ri || li > ri) return 0;
  if (l == li && r == ri) return (mx[u] == n) ? num[u] : 0;
  int mid = (l + r) >> 1;
  down(u);
  return query(u << 1, l, mid, l, min(mid, ri)) +
         query((u << 1) | 1, mid + 1, r, max(mid + 1, li), ri);
}
}  // namespace Seg
int main() {
  n = read();
  Seg::build(1, 1, n);
  for (int i = 1; i <= n; i++) {
    V[i].push_back(0);
    Seg::update(1, 1, n, 1, n, 1);
  }
  for (int i = 1; i <= n; i++) {
    int key = read(), siz = V[key].size();
    if (siz >= 4)
      Seg::update(1, 1, n, V[key][siz - 4] + 1, V[key][siz - 3], -1);
    if (siz >= 3) Seg::update(1, 1, n, V[key][siz - 3] + 1, V[key][siz - 2], 1);
    Seg::update(1, 1, n, V[key][siz - 1] + 1, i, -1);
    V[key].push_back(i);
    ans += Seg::query(1, 1, n, 1, i);
  }
  printf("%lld\n", ans);
  return 0;
}
