#include <bits/stdc++.h>
using namespace std;
const int MAXN = 300100;
vector<long long> g[MAXN];
long long pfx[MAXN];
long long v[MAXN];
long long me[MAXN], md[MAXN];
long long q[MAXN];
long long pai[MAXN];
long long t[MAXN];
long long resp = 0;
long long n;
long long find(long long x) {
  if (pai[x] == x) return x;
  return pai[x] = find(pai[x]);
}
void join(long long a, long long b, long long p1, long long p2, long long k) {
  long long x = find(a), y = find(b);
  if (t[x] >= t[y]) {
    for (long long i = p2; i < p2 + t[y]; i++) {
      if (i - k + 1 > 0 && i - k + 1 >= p1 - t[x] + 1 && i - k + 1 <= p1 &&
          pfx[i] - pfx[i - k] == (k * (k + 1)) / 2 && me[i] <= i - k + 1)
        resp++;
    }
    t[x] += t[y];
    pai[y] = x;
  } else {
    for (long long i = p1; i >= p1 - t[x] + 1; i--) {
      if (i + k - 1 <= n && i + k - 1 <= p2 + t[y] - 1 && i + k - 1 >= p2 &&
          pfx[i + k - 1] - pfx[i - 1] == (k * (k + 1)) / 2 &&
          md[i] >= i + k - 1)
        resp++;
    }
    t[y] += t[x];
    pai[x] = y;
  }
}
signed main() {
  memset(pfx, 0, sizeof pfx);
  cin >> n;
  for (long long i = 1; i <= n; i++)
    cin >> v[i], pfx[i] = pfx[i - 1] + v[i], g[v[i]].push_back(i);
  for (long long i = 1; i <= n; i++) pai[i] = i, t[i] = 1;
  memset(q, 0, sizeof q);
  long long pnt = 1;
  for (long long i = 1; i <= n; i++) {
    q[v[i]]++;
    if (q[v[i]] == 2) {
      while (v[pnt] != v[i]) q[v[pnt]]--, pnt++;
      pnt++;
    }
    q[v[i]] = 1;
    me[i] = pnt;
  }
  memset(q, 0, sizeof q);
  pnt = n;
  for (long long i = n; i >= 1; i--) {
    q[v[i]]++;
    if (q[v[i]] == 2) {
      while (v[pnt] != v[i]) q[v[pnt]]--, pnt--;
      pnt--;
    }
    q[v[i]] = 1;
    md[i] = pnt;
  }
  for (long long i = 1; i <= n; i++) {
    for (long long j = 0; j < g[i].size(); j++) {
      if (i == 1) resp++;
      long long at = g[i][j];
      if (at + 1 <= n && v[at + 1] < i) join(at, at + 1, at, at + 1, i);
      if (at - 1 > 0 && v[at - 1] < i) join(at - 1, at, at - 1, at, i);
    }
  }
  cout << resp << endl;
}

### 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;
const int MAXN = 300100;
vector<long long> g[MAXN];
long long pfx[MAXN];
long long v[MAXN];
long long me[MAXN], md[MAXN];
long long q[MAXN];
long long pai[MAXN];
long long t[MAXN];
long long resp = 0;
long long n;
long long find(long long x) {
  if (pai[x] == x) return x;
  return pai[x] = find(pai[x]);
}
void join(long long a, long long b, long long p1, long long p2, long long k) {
  long long x = find(a), y = find(b);
  if (t[x] >= t[y]) {
    for (long long i = p2; i < p2 + t[y]; i++) {
      if (i - k + 1 > 0 && i - k + 1 >= p1 - t[x] + 1 && i - k + 1 <= p1 &&
          pfx[i] - pfx[i - k] == (k * (k + 1)) / 2 && me[i] <= i - k + 1)
        resp++;
    }
    t[x] += t[y];
    pai[y] = x;
  } else {
    for (long long i = p1; i >= p1 - t[x] + 1; i--) {
      if (i + k - 1 <= n && i + k - 1 <= p2 + t[y] - 1 && i + k - 1 >= p2 &&
          pfx[i + k - 1] - pfx[i - 1] == (k * (k + 1)) / 2 &&
          md[i] >= i + k - 1)
        resp++;
    }
    t[y] += t[x];
    pai[x] = y;
  }
}
signed main() {
  memset(pfx, 0, sizeof pfx);
  cin >> n;
  for (long long i = 1; i <= n; i++)
    cin >> v[i], pfx[i] = pfx[i - 1] + v[i], g[v[i]].push_back(i);
  for (long long i = 1; i <= n; i++) pai[i] = i, t[i] = 1;
  memset(q, 0, sizeof q);
  long long pnt = 1;
  for (long long i = 1; i <= n; i++) {
    q[v[i]]++;
    if (q[v[i]] == 2) {
      while (v[pnt] != v[i]) q[v[pnt]]--, pnt++;
      pnt++;
    }
    q[v[i]] = 1;
    me[i] = pnt;
  }
  memset(q, 0, sizeof q);
  pnt = n;
  for (long long i = n; i >= 1; i--) {
    q[v[i]]++;
    if (q[v[i]] == 2) {
      while (v[pnt] != v[i]) q[v[pnt]]--, pnt--;
      pnt--;
    }
    q[v[i]] = 1;
    md[i] = pnt;
  }
  for (long long i = 1; i <= n; i++) {
    for (long long j = 0; j < g[i].size(); j++) {
      if (i == 1) resp++;
      long long at = g[i][j];
      if (at + 1 <= n && v[at + 1] < i) join(at, at + 1, at, at + 1, i);
      if (at - 1 > 0 && v[at - 1] < i) join(at - 1, at, at - 1, at, i);
    }
  }
  cout << resp << endl;
}

```