#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = 1 << 19;
struct Node {
  int l, r;
  pair<int, int> val;
  int toAdd;
  Node() : l(-1), r(-1), val(make_pair(N, 1)), toAdd(0) {}
  Node(int _l, int _r) : l(_l), r(_r), val(make_pair(N, _r - _l)), toAdd(0) {}
  void add(int x) {
    toAdd += x;
    val.first += x;
  }
};
Node tree[2 * N + 3];
void build() {
  for (int i = 0; i < N; i++) tree[N + i] = Node(i, i + 1);
  for (int i = N - 1; i > 0; i--)
    tree[i] = Node(tree[2 * i].l, tree[2 * i + 1].r);
}
void push(int v) {
  if (v >= N) throw;
  for (int u = 2 * v; u < 2 * v + 2; u++) {
    tree[u].add(tree[v].toAdd);
  }
  tree[v].toAdd = 0;
}
pair<int, int> merge(pair<int, int> v, pair<int, int> u) {
  pair<int, int> res;
  res.first = min(v.first, u.first);
  res.second = 0;
  if (res.first == v.first) res.second += v.second;
  if (res.first == u.first) res.second += u.second;
  return res;
}
void update(int v) {
  if (v >= N) throw;
  tree[v].val = merge(tree[2 * v].val, tree[2 * v + 1].val);
}
void addOnSegm(int v, int l, int r, int x) {
  if (l <= tree[v].l && tree[v].r <= r) {
    tree[v].add(x);
    return;
  }
  if (l >= tree[v].r || tree[v].l >= r) return;
  push(v);
  for (int u = 2 * v; u < 2 * v + 2; u++) addOnSegm(u, l, r, x);
  update(v);
}
pair<int, int> getVal(int v, int l, int r) {
  if (l <= tree[v].l && tree[v].r <= r) return tree[v].val;
  if (l >= tree[v].r || tree[v].l >= r) return make_pair(N, 0);
  push(v);
  return merge(getVal(2 * v, l, r), getVal(2 * v + 1, l, r));
}
int n;
int a[N + 5];
int lst[N + 5];
int st[N + 5][2];
int stSz;
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", &a[i]);
  for (int i = 0; i < N; i++) lst[i] = -1;
  build();
  st[stSz][0] = N;
  st[stSz][1] = -1;
  stSz++;
  long long ans = 0;
  int L = 0;
  for (int r = 1; r <= n; r++) {
    int x = a[r - 1];
    L = max(L, lst[x] + 1);
    lst[x] = r - 1;
    int p = r - 1;
    while (st[stSz - 1][0] < x) {
      stSz--;
      addOnSegm(1, st[stSz][1], p, -st[stSz][0]);
      p = st[stSz][1];
    }
    addOnSegm(1, p, r, x);
    st[stSz][0] = x;
    st[stSz][1] = p;
    stSz++;
    addOnSegm(1, r - 1, r, r - 1 - N);
    pair<int, int> val = getVal(1, L, r);
    if (val.first == r) ans += val.second;
  }
  printf("%lld\n", ans);
  return 0;
}

### Prompt

Create a solution in CPP for the following problem:

You have an array a_1, a_2, ..., a_n. 

Let's call some subarray a_l, a_{l + 1}, ... , a_r of this array a subpermutation if it contains all integers from 1 to r-l+1 exactly once. For example, array a = [2, 2, 1, 3, 2, 3, 1] contains 6 subarrays which are subpermutations: [a_2 ... a_3], [a_2 ... a_4], [a_3 ... a_3], [a_3 ... a_5], [a_5 ... a_7], [a_7 ... a_7].

You are asked to calculate the number of subpermutations.

Input

The first line contains one integer n (1 ≤ n ≤ 3 ⋅ 10^5).

The second line contains n integers a_1, a_2, ... , a_n (1 ≤ a_i ≤ n). 

This array can contain the same integers.

Output

Print the number of subpermutations of the array a.

Examples

Input


8
2 4 1 3 4 2 1 2


Output


7


Input


5
1 1 2 1 2


Output


6

Note

There are 7 subpermutations in the first test case. Their segments of indices are [1, 4], [3, 3], [3, 6], [4, 7], [6, 7], [7, 7] and [7, 8].

In the second test case 6 subpermutations exist: [1, 1], [2, 2], [2, 3], [3, 4], [4, 4] and [4, 5].

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = 1 << 19;
struct Node {
  int l, r;
  pair<int, int> val;
  int toAdd;
  Node() : l(-1), r(-1), val(make_pair(N, 1)), toAdd(0) {}
  Node(int _l, int _r) : l(_l), r(_r), val(make_pair(N, _r - _l)), toAdd(0) {}
  void add(int x) {
    toAdd += x;
    val.first += x;
  }
};
Node tree[2 * N + 3];
void build() {
  for (int i = 0; i < N; i++) tree[N + i] = Node(i, i + 1);
  for (int i = N - 1; i > 0; i--)
    tree[i] = Node(tree[2 * i].l, tree[2 * i + 1].r);
}
void push(int v) {
  if (v >= N) throw;
  for (int u = 2 * v; u < 2 * v + 2; u++) {
    tree[u].add(tree[v].toAdd);
  }
  tree[v].toAdd = 0;
}
pair<int, int> merge(pair<int, int> v, pair<int, int> u) {
  pair<int, int> res;
  res.first = min(v.first, u.first);
  res.second = 0;
  if (res.first == v.first) res.second += v.second;
  if (res.first == u.first) res.second += u.second;
  return res;
}
void update(int v) {
  if (v >= N) throw;
  tree[v].val = merge(tree[2 * v].val, tree[2 * v + 1].val);
}
void addOnSegm(int v, int l, int r, int x) {
  if (l <= tree[v].l && tree[v].r <= r) {
    tree[v].add(x);
    return;
  }
  if (l >= tree[v].r || tree[v].l >= r) return;
  push(v);
  for (int u = 2 * v; u < 2 * v + 2; u++) addOnSegm(u, l, r, x);
  update(v);
}
pair<int, int> getVal(int v, int l, int r) {
  if (l <= tree[v].l && tree[v].r <= r) return tree[v].val;
  if (l >= tree[v].r || tree[v].l >= r) return make_pair(N, 0);
  push(v);
  return merge(getVal(2 * v, l, r), getVal(2 * v + 1, l, r));
}
int n;
int a[N + 5];
int lst[N + 5];
int st[N + 5][2];
int stSz;
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", &a[i]);
  for (int i = 0; i < N; i++) lst[i] = -1;
  build();
  st[stSz][0] = N;
  st[stSz][1] = -1;
  stSz++;
  long long ans = 0;
  int L = 0;
  for (int r = 1; r <= n; r++) {
    int x = a[r - 1];
    L = max(L, lst[x] + 1);
    lst[x] = r - 1;
    int p = r - 1;
    while (st[stSz - 1][0] < x) {
      stSz--;
      addOnSegm(1, st[stSz][1], p, -st[stSz][0]);
      p = st[stSz][1];
    }
    addOnSegm(1, p, r, x);
    st[stSz][0] = x;
    st[stSz][1] = p;
    stSz++;
    addOnSegm(1, r - 1, r, r - 1 - N);
    pair<int, int> val = getVal(1, L, r);
    if (val.first == r) ans += val.second;
  }
  printf("%lld\n", ans);
  return 0;
}

```