#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 7;
const long long mod = 998244353;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int n;
int a[N];
vector<int> v[N];
int t[N * 4];
int lz[N * 4];
int res[N * 4];
void push(int x) {
  lz[x + x] += lz[x];
  lz[x + x + 1] += lz[x];
  t[x + x] += lz[x];
  t[x + x + 1] += lz[x];
  lz[x] = 0;
}
void meg(int x) {
  t[x] = max(t[x + x], t[x + x + 1]);
  res[x] = res[x + x];
  if (t[x + x] == t[x + x + 1]) {
    res[x] += res[x + x + 1];
  } else if (t[x + x + 1] > t[x + x]) {
    res[x] = res[x + x + 1];
  }
}
void upd(int x, int l, int r, int tl, int tr, int val) {
  if (tl > tr) return;
  if (l == tl && r == tr) {
    lz[x] += val;
    t[x] += val;
    return;
  }
  int m = (l + r) / 2;
  push(x);
  upd(x + x, l, m, tl, min(tr, m), val);
  upd(x + x + 1, m + 1, r, max(tl, m + 1), tr, val);
  meg(x);
}
void foo(int x, int l, int r) {
  res[x] = r - l + 1;
  if (l == r) return;
  int m = (l + r) / 2;
  foo(x + x, l, m);
  foo(x + x + 1, m + 1, r);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  long long ans = 0;
  int G = 0;
  for (int i = 1; i <= n; i++) {
    v[i].push_back(0);
  }
  foo(1, 1, n);
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    v[x].push_back(i);
    if ((int)v[x].size() == 2) G++;
    int l = (int)v[x].size();
    if (l > 2) {
      upd(1, 1, n, v[x][l - 2] + 1, n, -1);
      upd(1, 1, n, i + 1, n, 1);
    }
    if (l == 2) {
      upd(1, 1, n, v[x][l - 1] + 1, n, 1);
    }
    if (l > 4) {
      upd(1, 1, n, v[x][l - 5] + 1, v[x][l - 4], -1);
      upd(1, 1, n, v[x][l - 4] + 1, v[x][l - 3], 1);
    }
    if (l == 4) {
      upd(1, 1, n, v[x][l - 4] + 1, v[x][l - 3], 1);
    }
    if (t[1] == G) {
      ans += res[1] - (n - i);
    }
  }
  cout << ans;
}
