#include <bits/stdc++.h>
using namespace std;
const int maxN = 5e5 + 10;
struct Node {
  int cov, len;
} tree[maxN * 4 + 1];
int n;
int a[maxN + 1];
int nxt[maxN + 1][4];
inline void pushup(int node, int l, int r) {
  if (tree[node].cov)
    tree[node].len = r - l + 1;
  else
    tree[node].len = l == r ? 0 : tree[node << 1].len + tree[node << 1 | 1].len;
}
inline void modify(int node, int l, int r, int x, int y, int v) {
  if (x <= l && r <= y) {
    tree[node].cov += v;
    pushup(node, l, r);
    return;
  }
  int mid = (l + r) >> 1;
  if (x <= mid) modify(node << 1, l, mid, x, y, v);
  if (y > mid) modify(node << 1 | 1, mid + 1, r, x, y, v);
  pushup(node, l, r);
}
inline int query(int node, int l, int r, int x, int y) {
  if (tree[node].cov) return min(r, y) - max(l, x) + 1;
  if (x <= l && r <= y) return tree[node].len;
  int mid = (l + r) >> 1, ans = 0;
  if (x <= mid) ans += query(node << 1, l, mid, x, y);
  if (y > mid) ans += query(node << 1 | 1, mid + 1, r, x, y);
  return ans;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    nxt[i][0] = nxt[i][1] = nxt[i][2] = nxt[i][3] = n + 1;
  }
  long long ans = 0;
  int r = n + 1;
  for (int i = n; i >= 1; i--) {
    int c = a[i];
    if (nxt[c][0] < nxt[c][2]) modify(1, 1, n, nxt[c][0], nxt[c][2] - 1, -1);
    nxt[c][3] = nxt[c][2], nxt[c][2] = nxt[c][1];
    nxt[c][1] = nxt[c][0], nxt[c][0] = i;
    if (nxt[c][0] < nxt[c][2]) modify(1, 1, n, nxt[c][0], nxt[c][2] - 1, 1);
    r = min(r, nxt[c][3]);
    ans += r - i - query(1, 1, n, i, r - 1);
  }
  printf("%lld\n", ans);
  return 0;
}
