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;
struct point {
  long long x, y;
  point() {
    x = 0;
    y = 0;
  }
  point(long long x, long long y) : x(x), y(y) {}
  int quarter() const {
    if (x >= 0 && y >= 0) return 0;
    if (x <= 0 && y >= 0) return 1;
    if (x <= 0 && y <= 0) return 2;
    if (x >= 0 && y <= 0) return 3;
    assert(false);
    return -1;
  }
};
point operator-(const point &a, const point &b) {
  return point(a.x - b.x, a.y - b.y);
}
point operator+(const point &a, const point &b) {
  return point(a.x + b.x, a.y + b.y);
}
long long cross(const point &a, const point &b) {
  return a.x * b.y - a.y * b.x;
}
long long dot(const point &a, const point &b) { return a.x * b.x + a.y * b.y; }
bool operator<(pair<point, int> c, pair<point, int> d) {
  if (c.first.quarter() != d.first.quarter())
    return c.first.quarter() < d.first.quarter();
  long long cr = cross(c.first, d.first);
  if (cr != 0)
    return cr < 0;
  else
    return c.second < d.second;
}
mt19937_64 rng(0);
int main() {
  int n;
  cin >> n;
  long long x[n + 1], y[n + 1];
  vector<int> b(n + 1, 0);
  for (int i = 0; i <= n; i++) {
    x[i] = rng();
    y[i] = rng();
  }
  long long h[n + 1];
  h[0] = 0;
  vector<int> P[n + 1];
  long long ans = 0;
  map<long long, long long> M;
  int id = 0;
  M[h[0]]++;
  for (int i = 1; i <= n; i++) {
    h[i] = h[i - 1];
    int a;
    cin >> a;
    b[a]++;
    if (b[a] % 3 == 0)
      h[i] ^= x[a];
    else if (b[a] % 3 == 1)
      h[i] ^= y[a];
    else {
      h[i] ^= x[a];
      h[i] ^= y[a];
    }
    if (P[a].size() >= 4) {
      int x = P[a][P[a].size() - 4];
      while (id + 1 <= x) {
        M[h[id]]--;
        id++;
      }
    }
    ans += M[h[i]];
    M[h[i]]++;
    P[a].push_back(i);
  }
  cout << ans << "\n";
}
