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 N = 500010;
int n;
int a[N];
int nxt[4][N];
int lastPos[N];
struct IT {
  int minVal[N << 2];
  int numMinVal[N << 2];
  int lazy[N << 2];
  void pull(int node, int l, int r) {
    if (minVal[node << 1] == minVal[node << 1 | 1]) {
      minVal[node] = minVal[node << 1];
      numMinVal[node] = numMinVal[node << 1] + numMinVal[node << 1 | 1];
    } else {
      int id =
          minVal[node << 1] < minVal[node << 1 | 1] ? node << 1 : node << 1 | 1;
      minVal[node] = minVal[id];
      numMinVal[node] = numMinVal[id];
    }
  }
  void push(int node, int l, int r, int m) {
    for (int it = 0; it < 2; it++) {
      minVal[node << 1 | it] += lazy[node];
      lazy[node << 1 | it] += lazy[node];
    }
    lazy[node] = 0;
  }
  void init(int node, int l, int r) {
    lazy[node] = 0;
    if (l == r) {
      minVal[node] = 0;
      numMinVal[node] = 1;
      return;
    }
    int m = l + r >> 1;
    init(node << 1, l, m);
    init(node << 1 | 1, m + 1, r);
    pull(node, l, r);
  }
  void update(int node, int l, int r, int x, int y, int delta) {
    if (x > r || y < l) return;
    if (x <= l && r <= y) {
      minVal[node] += delta;
      lazy[node] += delta;
      return;
    }
    int m = l + r >> 1;
    push(node, l, r, m);
    update(node << 1, l, m, x, y, delta);
    update(node << 1 | 1, m + 1, r, x, y, delta);
    pull(node, l, r);
  }
  int get(int node, int l, int r, int x, int y) {
    if (x > r || y < l) return 0;
    if (x <= l && r <= y) {
      if (minVal[node] == 0) return numMinVal[node];
      return 0;
    }
    int m = l + r >> 1;
    push(node, l, r, m);
    int p1 = get(node << 1, l, m, x, y);
    int p2 = get(node << 1 | 1, m + 1, r, x, y);
    return p1 + p2;
  }
} t;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  fill(lastPos + 1, lastPos + n + 1, n + 1);
  for (int i = 0; i < 4; i++) nxt[i][n + 1] = n + 1;
  for (int i = n; i; i--) {
    nxt[0][i] = lastPos[a[i]];
    for (int j = 1; j < 4; j++) {
      nxt[j][i] = nxt[j - 1][lastPos[a[i]]];
    }
    lastPos[a[i]] = i;
  }
  t.init(1, 1, n);
  long long res = 0;
  for (int i = n; i; i--) {
    t.update(1, 1, n, i, nxt[0][i] - 1, +1);
    t.update(1, 1, n, nxt[1][i], nxt[2][i] - 1, -1);
    t.update(1, 1, n, nxt[2][i], nxt[3][i] - 1, +1);
    res += t.get(1, 1, n, i, n);
  }
  cout << res << endl;
  return 0;
}
