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;
using ll = long long;
using ld = long double;
using u64 = uint64_t;
using u32 = uint32_t;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vpi = vector<pi>;
static constexpr int INF = (int)1e9 + 5;
static constexpr ll INFL = (ll)1e18 + 5;
static mt19937 rng((u32)chrono::duration_cast<chrono::nanoseconds>(
                       chrono::high_resolution_clock::now().time_since_epoch())
                       .count());
struct SegTree {
  vi avail, blockcnt;
  explicit SegTree(int n) : avail(4 * n), blockcnt(4 * n) {}
  int get(int i) { return avail[i] * (blockcnt[i] == 0); }
  void setavail(int i, int l, int r, int qi) {
    if (r - l == 1) {
      avail[i] = 1;
      return;
    }
    int m = (l + r) / 2;
    if (qi < m)
      setavail(2 * i, l, m, qi);
    else
      setavail(2 * i + 1, m, r, qi);
    avail[i] = get(2 * i) + get(2 * i + 1);
  }
  void update(int i, int l, int r, int ql, int qr, int val) {
    if (qr <= l || r <= ql) return;
    if (ql <= l && r <= qr) {
      blockcnt[i] += val;
      return;
    }
    int m = (l + r) / 2;
    update(2 * i, l, m, ql, qr, val);
    update(2 * i + 1, m, r, ql, qr, val);
    avail[i] = get(2 * i) + get(2 * i + 1);
  }
  int query(int i, int l, int r, int ql, int qr) {
    if (qr <= l || r <= ql) return 0;
    if (ql <= l && r <= qr) return get(i);
    int m = (l + r) / 2;
    return (blockcnt[i] == 0) *
           (query(2 * i, l, m, ql, qr) + query(2 * i + 1, m, r, ql, qr));
  }
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  vi a(n);
  for (auto& ai : a) cin >> ai, --ai;
  vector<array<int, 4>> pos(n, array<int, 4>{n, n, n, n});
  int mn4 = n;
  SegTree s(n + 1);
  for (int i = 0; i < (n + 1); ++i) s.setavail(1, 0, n + 1, i);
  ll c = 0;
  for (int i = n - 1; i >= 0; --i) {
    auto& p = pos[a[i]];
    s.update(1, 0, n + 1, p[0] + 1, p[2] + 1, -1);
    rotate(begin(p), end(p) - 1, end(p));
    p[0] = i;
    s.update(1, 0, n + 1, p[0] + 1, p[2] + 1, 1);
    mn4 = min(mn4, p[3]);
    c += s.query(1, 0, n + 1, i + 1, mn4 + 1);
  }
  cout << c << '\n';
  return 0;
}
