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 MOD = (int)1e9 + 7;
const int FFTMOD = 119 << 23 | 1;
const int INF = (int)1e9 + 23111992;
const long long LINF = (long long)1e18 + 23111992;
const long double PI = acos((long double)-1);
const long double EPS = 1e-9;
inline long long gcd(long long a, long long b) {
  long long r;
  while (b) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}
inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
inline long long fpow(long long n, long long k, int p = MOD) {
  long long r = 1;
  for (; k; k >>= 1) {
    if (k & 1) r = r * n % p;
    n = n * n % p;
  }
  return r;
}
template <class T>
inline int chkmin(T& a, const T& val) {
  return val < a ? a = val, 1 : 0;
}
template <class T>
inline int chkmax(T& a, const T& val) {
  return a < val ? a = val, 1 : 0;
}
inline unsigned long long isqrt(unsigned long long k) {
  unsigned long long r = sqrt(k) + 1;
  while (r * r > k) r--;
  return r;
}
inline long long icbrt(long long k) {
  long long r = cbrt(k) + 1;
  while (r * r * r > k) r--;
  return r;
}
inline void addmod(int& a, int val, int p = MOD) {
  if ((a = (a + val)) >= p) a -= p;
}
inline void submod(int& a, int val, int p = MOD) {
  if ((a = (a - val)) < 0) a += p;
}
inline int mult(int a, int b, int p = MOD) { return (long long)a * b % p; }
inline int inv(int a, int p = MOD) { return fpow(a, p - 2, p); }
inline int sign(long double x) { return x < -EPS ? -1 : x > +EPS; }
inline int sign(long double x, long double y) { return sign(x - y); }
mt19937 mt(chrono::high_resolution_clock::now().time_since_epoch().count());
inline int mrand() { return abs((int)mt()); }
inline int mrand(int k) { return abs((int)mt()) % k; }
template <class TK, class TV>
struct HashTable {
  static const int MAXN = 10000000;
  static const int MAGIC = 7654321;
  int ptr;
  int lst[MAGIC];
  int prv[MAXN];
  TK l[MAXN];
  TV x[MAXN];
  int cnt;
  int used[MAGIC];
  HashTable() {
    ptr = 0;
    fill_n(lst, MAGIC, -1);
    cnt = 0;
  }
  void clear() {
    ptr = 0;
    for (int i = 0; i < cnt; i++) {
      lst[used[i]] = -1;
    }
    cnt = 0;
  }
  TV& operator[](const TK& key) {
    int hs = (key % MAGIC + MAGIC) % MAGIC;
    if (!~lst[hs]) {
      used[cnt++] = hs;
    }
    for (int e = lst[hs]; ~e; e = prv[e]) {
      if (l[e] == key) {
        return x[e];
      }
    }
    l[ptr] = key, x[ptr] = 0;
    prv[ptr] = lst[hs], lst[hs] = ptr;
    return x[ptr++];
  }
  int count(TK key) {
    int hs = (key % MAGIC + MAGIC) % MAGIC;
    for (int e = lst[hs]; ~e; e = prv[e]) {
      if (l[e] == key) {
        return 1;
      }
    }
    return 0;
  }
  void erase(TK key) {
    int hs = (key % MAGIC + MAGIC) % MAGIC;
    int nxt = -1;
    for (int e = lst[hs]; ~e; e = prv[e]) {
      if (l[e] == key) {
        if (!~nxt) {
          lst[hs] = prv[e];
        } else {
          prv[nxt] = prv[e];
        }
        return;
      }
      nxt = e;
    }
  }
};
HashTable<long long, int> hs;
void chemthan() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = (0); i < (n); ++i) cin >> a[i], a[i]--;
  vector<long long> vals(n);
  for (int i = (0); i < (n); ++i) vals[i] = (long long)mrand() * mrand();
  vector<int> cnt(n);
  long long t = 0;
  vector<long long> f(n);
  for (int i = (0); i < (n); ++i) {
    t -= cnt[a[i]] * vals[a[i]];
    cnt[a[i]]++;
    cnt[a[i]] %= 3;
    t += cnt[a[i]] * vals[a[i]];
    f[i] = t;
  }
  fill((cnt).begin(), (cnt).end(), 0);
  int ptr = 0;
  long long res = 0;
  hs[0]++;
  for (int i = (0); i < (n); ++i) {
    while (ptr < n && cnt[a[ptr]] < 3) {
      cnt[a[ptr]]++;
      hs[f[ptr]]++;
      ptr++;
    }
    long long t = i ? f[i - 1] : 0;
    hs[t]--;
    cnt[a[i]]--;
    res += hs[t];
  }
  cout << res << "\n";
}
int main(int argc, char* argv[]) {
  ios_base::sync_with_stdio(0), cin.tie(0);
  if (argc > 1) {
    assert(freopen(argv[1], "r", stdin));
  }
  if (argc > 2) {
    assert(freopen(argv[2], "wb", stdout));
  }
  chemthan();
  cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
  return 0;
}
