#include <bits/stdc++.h>
using namespace std;
template <class Types>
inline Types Max(Types a, Types b) {
  return (a > b) ? a : b;
}
template <class Types>
inline Types Min(Types a, Types b) {
  return (a < b) ? a : b;
}
int n;
long long ans;
int a[300005], st[300005];
int lst[300005], p[300005];
int mx[300005][22], mxl[300005][22];
int ln[300005], sum[300005];
int lef[300005], rig[300005];
struct node {
  int id, data;
  friend bool operator<(const node n1, const node n2) {
    return n1.data > n2.data;
  }
} b[300005], s[300005];
inline int read() {
  char ch = getchar();
  int x = 0, f = 1;
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') f = -1;
  for (; isdigit(ch); ch = getchar()) x = (x << 3) + (x << 1) + (ch ^ 48);
  return x * f;
}
void pretreat() {
  for (int i = (1); i <= (n); ++i) mx[i][0] = a[i];
  for (int j = (1); j <= (20); ++j)
    for (int i = (1); i <= (n); ++i) {
      mx[i][j] = Max(mx[i][j - 1], mx[Min(i + (1 << j - 1), n)][j - 1]);
    }
  int l = 0;
  for (int i = (1); i <= (n); ++i) ln[i] = (1 << l + 1) == i ? ++l : l;
}
int Mx(int l, int r) {
  int k = ln[r - l + 1];
  return Max(mx[l][k], mx[r - (1 << k) + 1][k]);
}
int check1(int l, int i) {
  int r = i;
  while (l < r) {
    int mid = l + r >> 1;
    if (Mx(mid, i) > a[i])
      l = mid + 1;
    else
      r = mid;
  }
  return l;
}
int check2(int i, int r) {
  r = Min(r, n);
  int l = i;
  while (l < r) {
    int mid = l + r + 1 >> 1;
    if (Mx(i, mid) > a[i])
      r = mid - 1;
    else
      l = mid;
  }
  return l;
}
int lowbit(int x) { return x & (-x); }
void updata(int x, int k) {
  for (int i = x; i <= n; i += lowbit(i)) sum[i] += k;
}
int query(int x) {
  int ans = 0;
  for (int i = x; i >= 1; i -= lowbit(i)) ans += sum[i];
  return ans;
}
int main() {
  n = read();
  for (int i = (1); i <= (n); ++i) {
    a[i] = read();
    b[i].id = i;
    b[i].data = a[i];
    lst[i] = p[a[i]];
    p[a[i]] = i;
  }
  int now = 0;
  for (int i = (1); i <= (n); ++i) {
    while (now < n && lst[now + 1] < i) ++now;
    st[i] = now;
    s[i].id = i;
    s[i].data = st[i] - i + 1;
  }
  now = 0;
  for (int i = (1); i <= (n); ++i) {
    if (a[i] == 1) now = i;
    lef[i] = now;
  }
  now = 0;
  for (int i = (n); i >= (1); --i) {
    if (a[i] == 1) now = i;
    rig[i] = now;
  }
  pretreat();
  sort(b + 1, b + 1 + n);
  sort(s + 1, s + 1 + n);
  now = 1;
  for (int i = (1); i <= (n); ++i) {
    int x = b[i].data, k = b[i].id;
    int l = Max(1, k - x + 1), r = Min(n - x + 1, k);
    l = Max(check1(l, k), l);
    r = Min(check2(k, r + x - 1) - x + 1, r);
    while (now <= n && s[now].data >= x) updata(s[now].id, 1), ++now;
    int px = lef[k], py = rig[k] - x + 1;
    px = Min(px, r), py = Max(py, l);
    if (l > r) {
      continue;
    }
    if (px < py)
      ans +=
          Max(0, query(px) - query(l - 1)) + Max(0, query(r) - query(py - 1));
    else
      ans += Max(0, query(r) - query(l - 1));
  }
  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;
template <class Types>
inline Types Max(Types a, Types b) {
  return (a > b) ? a : b;
}
template <class Types>
inline Types Min(Types a, Types b) {
  return (a < b) ? a : b;
}
int n;
long long ans;
int a[300005], st[300005];
int lst[300005], p[300005];
int mx[300005][22], mxl[300005][22];
int ln[300005], sum[300005];
int lef[300005], rig[300005];
struct node {
  int id, data;
  friend bool operator<(const node n1, const node n2) {
    return n1.data > n2.data;
  }
} b[300005], s[300005];
inline int read() {
  char ch = getchar();
  int x = 0, f = 1;
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') f = -1;
  for (; isdigit(ch); ch = getchar()) x = (x << 3) + (x << 1) + (ch ^ 48);
  return x * f;
}
void pretreat() {
  for (int i = (1); i <= (n); ++i) mx[i][0] = a[i];
  for (int j = (1); j <= (20); ++j)
    for (int i = (1); i <= (n); ++i) {
      mx[i][j] = Max(mx[i][j - 1], mx[Min(i + (1 << j - 1), n)][j - 1]);
    }
  int l = 0;
  for (int i = (1); i <= (n); ++i) ln[i] = (1 << l + 1) == i ? ++l : l;
}
int Mx(int l, int r) {
  int k = ln[r - l + 1];
  return Max(mx[l][k], mx[r - (1 << k) + 1][k]);
}
int check1(int l, int i) {
  int r = i;
  while (l < r) {
    int mid = l + r >> 1;
    if (Mx(mid, i) > a[i])
      l = mid + 1;
    else
      r = mid;
  }
  return l;
}
int check2(int i, int r) {
  r = Min(r, n);
  int l = i;
  while (l < r) {
    int mid = l + r + 1 >> 1;
    if (Mx(i, mid) > a[i])
      r = mid - 1;
    else
      l = mid;
  }
  return l;
}
int lowbit(int x) { return x & (-x); }
void updata(int x, int k) {
  for (int i = x; i <= n; i += lowbit(i)) sum[i] += k;
}
int query(int x) {
  int ans = 0;
  for (int i = x; i >= 1; i -= lowbit(i)) ans += sum[i];
  return ans;
}
int main() {
  n = read();
  for (int i = (1); i <= (n); ++i) {
    a[i] = read();
    b[i].id = i;
    b[i].data = a[i];
    lst[i] = p[a[i]];
    p[a[i]] = i;
  }
  int now = 0;
  for (int i = (1); i <= (n); ++i) {
    while (now < n && lst[now + 1] < i) ++now;
    st[i] = now;
    s[i].id = i;
    s[i].data = st[i] - i + 1;
  }
  now = 0;
  for (int i = (1); i <= (n); ++i) {
    if (a[i] == 1) now = i;
    lef[i] = now;
  }
  now = 0;
  for (int i = (n); i >= (1); --i) {
    if (a[i] == 1) now = i;
    rig[i] = now;
  }
  pretreat();
  sort(b + 1, b + 1 + n);
  sort(s + 1, s + 1 + n);
  now = 1;
  for (int i = (1); i <= (n); ++i) {
    int x = b[i].data, k = b[i].id;
    int l = Max(1, k - x + 1), r = Min(n - x + 1, k);
    l = Max(check1(l, k), l);
    r = Min(check2(k, r + x - 1) - x + 1, r);
    while (now <= n && s[now].data >= x) updata(s[now].id, 1), ++now;
    int px = lef[k], py = rig[k] - x + 1;
    px = Min(px, r), py = Max(py, l);
    if (l > r) {
      continue;
    }
    if (px < py)
      ans +=
          Max(0, query(px) - query(l - 1)) + Max(0, query(r) - query(py - 1));
    else
      ans += Max(0, query(r) - query(l - 1));
  }
  printf("%lld\n", ans);
  return 0;
}

```