#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3e5 + 7;
struct nd {
  int mn, cnt;
};
nd tr[4 * MAXN];
int lazy[4 * MAXN];
nd combine(const nd &a, const nd &b) {
  if (a.mn < b.mn)
    return a;
  else if (a.mn > b.mn)
    return b;
  nd rt;
  rt.mn = a.mn;
  rt.cnt = a.cnt + b.cnt;
  return rt;
}
void propagate(int u, int l, int r) {
  if (lazy[u]) {
    tr[u].mn += lazy[u];
    if (l != r) {
      lazy[u * 2] += lazy[u];
      lazy[u * 2 + 1] += lazy[u];
    }
    lazy[u] = 0;
  }
}
int a[MAXN];
void build(int u, int l, int r) {
  lazy[u] = 0;
  if (l == r) {
    tr[u].mn = a[l];
    tr[u].cnt = 1;
    return;
  }
  int mid = (l + r) / 2;
  build(u * 2, l, mid);
  build(u * 2 + 1, mid + 1, r);
  tr[u] = combine(tr[u * 2], tr[u * 2 + 1]);
}
void update(int u, int l, int r, int x, int y, int v) {
  assert(x <= y);
  propagate(u, l, r);
  if (r < x || y < l) return;
  if (x <= l && r <= y) {
    lazy[u] += v;
    propagate(u, l, r);
    return;
  }
  int mid = (l + r) / 2;
  update(u * 2, l, mid, x, y, v);
  update(u * 2 + 1, mid + 1, r, x, y, v);
  tr[u] = combine(tr[u * 2], tr[u * 2 + 1]);
}
nd query(int u, int l, int r, int x, int y) {
  assert(x <= y);
  propagate(u, l, r);
  if (x <= l && r <= y) return tr[u];
  int mid = (l + r) / 2;
  if (y <= mid) return query(u * 2, l, mid, x, y);
  if (mid < x) return query(u * 2 + 1, mid + 1, r, x, y);
  return combine(query(u * 2, l, mid, x, y),
                 query(u * 2 + 1, mid + 1, r, x, y));
}
int p[MAXN];
long long ans;
int n;
void solve(int l, int mid, int r) {
  if (l == r) {
    ans += p[l] == 1;
    return;
  }
  set<int> st;
  int dan;
  for (dan = mid; dan + 1 <= r && st.count(p[dan + 1]) == 0; dan++) {
    st.insert(p[dan + 1]);
  }
  stack<int> minStack, maxStack;
  for (int i = dan; i > mid; i--) {
    while (!minStack.empty() && p[i] <= p[minStack.top()]) minStack.pop();
    minStack.push(i);
    while (!maxStack.empty() && p[i] >= p[maxStack.top()]) maxStack.pop();
    maxStack.push(i);
  }
  int mx = p[mid + 1], mn = p[mid + 1];
  for (int R = mid + 1; R <= dan; R++) {
    mx = max(mx, p[R]);
    mn = min(mn, p[R]);
    a[R - mid] = mx - mn - R;
  }
  int m = dan - mid;
  build(1, 1, m);
  {
    nd qr = query(1, 1, m, 1, m);
    assert(qr.mn >= -(mid + 1));
    if (qr.mn == -(mid + 1)) {
      ans += qr.cnt;
    }
  }
  for (int L = mid; L >= l; L--) {
    while (st.count(p[L]) == 1 && mid < dan) {
      st.erase(p[dan]);
      dan--;
    }
    if (dan <= mid) break;
    st.insert(p[L]);
    while (!minStack.empty() && minStack.top() <= dan &&
           p[L] <= p[minStack.top()]) {
      int x = minStack.top();
      minStack.pop();
      int y = minStack.empty() ? dan : minStack.top() - 1;
      int del = p[L] - p[x];
      update(1, 1, m, x - mid, min(y, dan) - mid, -del);
    }
    minStack.push(L);
    while (!maxStack.empty() && maxStack.top() <= dan &&
           p[L] >= p[maxStack.top()]) {
      int x = maxStack.top();
      maxStack.pop();
      int y = maxStack.empty() ? dan : maxStack.top() - 1;
      int del = p[L] - p[x];
      update(1, 1, m, x - mid, min(y, dan) - mid, del);
    }
    maxStack.push(L);
    nd q = query(1, 1, m, 1, dan - mid);
    assert(q.mn >= -L);
    if (q.mn == -L) {
      ans += q.cnt;
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  vector<int> ones;
  ones.push_back(0);
  for (int i = 1; i <= n; i++) {
    cin >> p[i];
    if (p[i] == 1) ones.push_back(i);
  }
  ones.push_back(n + 1);
  for (int i = 1; i + 1 < ones.size(); i++) {
    solve(ones[i - 1] + 1, ones[i] - 1, ones[i + 1] - 1);
  }
  cout << ans << endl;
  return 0;
}

### Prompt

Please formulate a Cpp solution to 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 = 3e5 + 7;
struct nd {
  int mn, cnt;
};
nd tr[4 * MAXN];
int lazy[4 * MAXN];
nd combine(const nd &a, const nd &b) {
  if (a.mn < b.mn)
    return a;
  else if (a.mn > b.mn)
    return b;
  nd rt;
  rt.mn = a.mn;
  rt.cnt = a.cnt + b.cnt;
  return rt;
}
void propagate(int u, int l, int r) {
  if (lazy[u]) {
    tr[u].mn += lazy[u];
    if (l != r) {
      lazy[u * 2] += lazy[u];
      lazy[u * 2 + 1] += lazy[u];
    }
    lazy[u] = 0;
  }
}
int a[MAXN];
void build(int u, int l, int r) {
  lazy[u] = 0;
  if (l == r) {
    tr[u].mn = a[l];
    tr[u].cnt = 1;
    return;
  }
  int mid = (l + r) / 2;
  build(u * 2, l, mid);
  build(u * 2 + 1, mid + 1, r);
  tr[u] = combine(tr[u * 2], tr[u * 2 + 1]);
}
void update(int u, int l, int r, int x, int y, int v) {
  assert(x <= y);
  propagate(u, l, r);
  if (r < x || y < l) return;
  if (x <= l && r <= y) {
    lazy[u] += v;
    propagate(u, l, r);
    return;
  }
  int mid = (l + r) / 2;
  update(u * 2, l, mid, x, y, v);
  update(u * 2 + 1, mid + 1, r, x, y, v);
  tr[u] = combine(tr[u * 2], tr[u * 2 + 1]);
}
nd query(int u, int l, int r, int x, int y) {
  assert(x <= y);
  propagate(u, l, r);
  if (x <= l && r <= y) return tr[u];
  int mid = (l + r) / 2;
  if (y <= mid) return query(u * 2, l, mid, x, y);
  if (mid < x) return query(u * 2 + 1, mid + 1, r, x, y);
  return combine(query(u * 2, l, mid, x, y),
                 query(u * 2 + 1, mid + 1, r, x, y));
}
int p[MAXN];
long long ans;
int n;
void solve(int l, int mid, int r) {
  if (l == r) {
    ans += p[l] == 1;
    return;
  }
  set<int> st;
  int dan;
  for (dan = mid; dan + 1 <= r && st.count(p[dan + 1]) == 0; dan++) {
    st.insert(p[dan + 1]);
  }
  stack<int> minStack, maxStack;
  for (int i = dan; i > mid; i--) {
    while (!minStack.empty() && p[i] <= p[minStack.top()]) minStack.pop();
    minStack.push(i);
    while (!maxStack.empty() && p[i] >= p[maxStack.top()]) maxStack.pop();
    maxStack.push(i);
  }
  int mx = p[mid + 1], mn = p[mid + 1];
  for (int R = mid + 1; R <= dan; R++) {
    mx = max(mx, p[R]);
    mn = min(mn, p[R]);
    a[R - mid] = mx - mn - R;
  }
  int m = dan - mid;
  build(1, 1, m);
  {
    nd qr = query(1, 1, m, 1, m);
    assert(qr.mn >= -(mid + 1));
    if (qr.mn == -(mid + 1)) {
      ans += qr.cnt;
    }
  }
  for (int L = mid; L >= l; L--) {
    while (st.count(p[L]) == 1 && mid < dan) {
      st.erase(p[dan]);
      dan--;
    }
    if (dan <= mid) break;
    st.insert(p[L]);
    while (!minStack.empty() && minStack.top() <= dan &&
           p[L] <= p[minStack.top()]) {
      int x = minStack.top();
      minStack.pop();
      int y = minStack.empty() ? dan : minStack.top() - 1;
      int del = p[L] - p[x];
      update(1, 1, m, x - mid, min(y, dan) - mid, -del);
    }
    minStack.push(L);
    while (!maxStack.empty() && maxStack.top() <= dan &&
           p[L] >= p[maxStack.top()]) {
      int x = maxStack.top();
      maxStack.pop();
      int y = maxStack.empty() ? dan : maxStack.top() - 1;
      int del = p[L] - p[x];
      update(1, 1, m, x - mid, min(y, dan) - mid, del);
    }
    maxStack.push(L);
    nd q = query(1, 1, m, 1, dan - mid);
    assert(q.mn >= -L);
    if (q.mn == -L) {
      ans += q.cnt;
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  vector<int> ones;
  ones.push_back(0);
  for (int i = 1; i <= n; i++) {
    cin >> p[i];
    if (p[i] == 1) ones.push_back(i);
  }
  ones.push_back(n + 1);
  for (int i = 1; i + 1 < ones.size(); i++) {
    solve(ones[i - 1] + 1, ones[i] - 1, ones[i + 1] - 1);
  }
  cout << ans << endl;
  return 0;
}

```