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>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#pragma GCC optimize("-ffloat-store")
using namespace std;
const auto start_time = std::chrono::high_resolution_clock::now();
void aryanc403() {}
const long long int INF = 0xFFFFFFFFFFFFFFFL;
long long int seed;
mt19937 rng(seed = chrono::steady_clock::now().time_since_epoch().count());
inline long long int rnd(long long int l = 0, long long int r = INF) {
  return uniform_int_distribution<long long int>(l, r)(rng);
}
class CMP {
 public:
  bool operator()(pair<long long int, long long int> a,
                  pair<long long int, long long int> b) {
    return !(a.first < b.first || (a.first == b.first && a.second <= b.second));
  }
};
void add(map<long long int, long long int> &m, long long int x,
         long long int cnt = 1) {
  auto jt = m.find(x);
  if (jt == m.end())
    m.insert({x, cnt});
  else
    jt->second += cnt;
}
void del(map<long long int, long long int> &m, long long int x,
         long long int cnt = 1) {
  auto jt = m.find(x);
  if (jt->second <= cnt)
    m.erase(jt);
  else
    jt->second -= cnt;
}
bool cmp(const pair<long long int, long long int> &a,
         const pair<long long int, long long int> &b) {
  return a.first < b.first || (a.first == b.first && a.second < b.second);
}
const long long int mod = 1000000007L;
const long long int L = 30;
long long int T, n, i, j, k, in, cnt, l, r, u, v, x, y;
long long int m;
string s;
vector<long long int> a;
vector<vector<long long int> > hashes, prefHash;
void getHash(vector<long long int> &a) {
  a.clear();
  a.resize(L);
  for (auto &x : a) x = rnd(0, 2);
}
void getXOR(vector<long long int> &ans, vector<long long int> &a,
            vector<long long int> &b) {
  for (long long int i = 0; i < L; ++i) ans[i] = (a[i] + b[i]) % 3;
}
int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  {
    cin >> n;
    hashes.resize(n + 1);
    for (auto &v : hashes) getHash(v);
    ;
    a.clear();
    a.reserve(n + 1);
    prefHash.resize(n + 1, vector<long long int>(L, 0));
    a.push_back(0);
    prefHash[0] = hashes[0];
    for (i = (1); i <= (n); ++i) {
      cin >> in;
      a.push_back(in);
      getXOR(prefHash[i], prefHash[i - 1], hashes[in]);
    };
    vector<long long int> freq(n + 1, 0);
    map<vector<long long int>, long long int> hval;
    long long int l = 1;
    long long int ans = 0;
    hval[prefHash[0]]++;
    for (long long int i = 1; i <= n; ++i) {
      freq[a[i]]++;
      while (l < i && freq[a[i]] > 3) {
        freq[a[l]]--;
        hval[prefHash[l - 1]]--;
        l++;
      };
      ans += hval[prefHash[i]];
      hval[prefHash[i]]++;
    }
    cout << ans << "\n";
  }
  aryanc403();
  return 0;
}
