#include <bits/stdc++.h>
using namespace std;
int n;
int a[300005];
int tree_min[1200000];
int tree_cnt[1200000];
int tree_max[1200000];
int nxt[300005];
int last[300005];
void build0(int i, int l, int r) {
  if (r - l == 1)
    tree_max[i] = a[l];
  else {
    int m = (r + l) / 2;
    build0(2 * i + 1, l, m);
    build0(2 * i + 2, m, r);
    tree_max[i] = max(tree_max[2 * i + 1], tree_max[2 * i + 2]);
  }
}
int get_max(int i, int l, int r, int lq, int rq) {
  if (lq >= r || l >= rq) return 0;
  if (lq <= l && r <= rq) return tree_max[i];
  int m = (r + l) / 2;
  return max(get_max(2 * i + 1, l, m, lq, rq),
             get_max(2 * i + 2, m, r, lq, rq));
}
void build(int i, int l, int r) {
  if (r - l == 1)
    tree_min[i] = a[l];
  else {
    int m = (r + l) / 2;
    build(2 * i + 1, l, m);
    build(2 * i + 2, m, r);
    tree_min[i] = min(tree_min[2 * i + 1], tree_min[2 * i + 2]);
  }
}
int get_min(int i, int l, int r, int lq, int rq) {
  if (lq >= r || l >= rq) return 1e9 + 7;
  if (lq <= l && r <= rq) return tree_min[i];
  int m = (r + l) / 2;
  return min(get_min(2 * i + 1, l, m, lq, rq),
             get_min(2 * i + 2, m, r, lq, rq));
}
void build1(int i, int l, int r) {
  if (r - l == 1) {
    tree_cnt[i] = nxt[l];
  } else {
    int m = (r + l) / 2;
    build1(2 * i + 1, l, m);
    build1(2 * i + 2, m, r);
    tree_cnt[i] = min(tree_cnt[2 * i + 1], tree_cnt[2 * i + 2]);
  }
}
int get_cnt(int i, int l, int r, int lq, int rq) {
  if (l >= rq || lq >= r) return 0;
  if (lq <= l && r <= rq) {
    if (tree_cnt[i] < rq) return 1;
    return 0;
  }
  int m = (r + l) / 2;
  return get_cnt(2 * i + 1, l, m, lq, rq) + get_cnt(2 * i + 2, m, r, lq, rq);
}
int f(int l, int r, int mx) {
  if (get_min(0, 1, n + 1, l, r + 1) == 1 &&
      get_max(0, 1, n + 1, l, r + 1) == mx &&
      get_cnt(0, 1, n + 1, l, r + 1) == 0)
    return 1;
  return 0;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> a[i];
  for (int i = n; i >= 1; --i) {
    if (last[a[i]] == 0)
      nxt[i] = n + 1;
    else
      nxt[i] = last[a[i]];
    last[a[i]] = i;
  }
  build0(0, 1, n + 1);
  build(0, 1, n + 1);
  build1(0, 1, n + 1);
  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    if (a[i] == 1) {
      ans++;
      int mx = 1;
      for (int j = i + 1; j <= n; ++j) {
        if (a[j] == 1) break;
        mx = max(mx, a[j]);
        if (mx <= j) {
          ans += f(j - mx + 1, j, mx);
        }
      }
    }
  }
  for (int i = n; i >= 1; --i) {
    if (a[i] == 1) {
      int mx = 1;
      for (int j = i - 1; j >= 1; --j) {
        if (a[j] == 1) break;
        mx = max(mx, a[j]);
        if (n - j + 1 >= mx) {
          ans += f(j, j + mx - 1, mx);
        }
      }
    }
  }
  cout << ans << '\n';
  return 0;
}

### Prompt

Generate 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;
int n;
int a[300005];
int tree_min[1200000];
int tree_cnt[1200000];
int tree_max[1200000];
int nxt[300005];
int last[300005];
void build0(int i, int l, int r) {
  if (r - l == 1)
    tree_max[i] = a[l];
  else {
    int m = (r + l) / 2;
    build0(2 * i + 1, l, m);
    build0(2 * i + 2, m, r);
    tree_max[i] = max(tree_max[2 * i + 1], tree_max[2 * i + 2]);
  }
}
int get_max(int i, int l, int r, int lq, int rq) {
  if (lq >= r || l >= rq) return 0;
  if (lq <= l && r <= rq) return tree_max[i];
  int m = (r + l) / 2;
  return max(get_max(2 * i + 1, l, m, lq, rq),
             get_max(2 * i + 2, m, r, lq, rq));
}
void build(int i, int l, int r) {
  if (r - l == 1)
    tree_min[i] = a[l];
  else {
    int m = (r + l) / 2;
    build(2 * i + 1, l, m);
    build(2 * i + 2, m, r);
    tree_min[i] = min(tree_min[2 * i + 1], tree_min[2 * i + 2]);
  }
}
int get_min(int i, int l, int r, int lq, int rq) {
  if (lq >= r || l >= rq) return 1e9 + 7;
  if (lq <= l && r <= rq) return tree_min[i];
  int m = (r + l) / 2;
  return min(get_min(2 * i + 1, l, m, lq, rq),
             get_min(2 * i + 2, m, r, lq, rq));
}
void build1(int i, int l, int r) {
  if (r - l == 1) {
    tree_cnt[i] = nxt[l];
  } else {
    int m = (r + l) / 2;
    build1(2 * i + 1, l, m);
    build1(2 * i + 2, m, r);
    tree_cnt[i] = min(tree_cnt[2 * i + 1], tree_cnt[2 * i + 2]);
  }
}
int get_cnt(int i, int l, int r, int lq, int rq) {
  if (l >= rq || lq >= r) return 0;
  if (lq <= l && r <= rq) {
    if (tree_cnt[i] < rq) return 1;
    return 0;
  }
  int m = (r + l) / 2;
  return get_cnt(2 * i + 1, l, m, lq, rq) + get_cnt(2 * i + 2, m, r, lq, rq);
}
int f(int l, int r, int mx) {
  if (get_min(0, 1, n + 1, l, r + 1) == 1 &&
      get_max(0, 1, n + 1, l, r + 1) == mx &&
      get_cnt(0, 1, n + 1, l, r + 1) == 0)
    return 1;
  return 0;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> a[i];
  for (int i = n; i >= 1; --i) {
    if (last[a[i]] == 0)
      nxt[i] = n + 1;
    else
      nxt[i] = last[a[i]];
    last[a[i]] = i;
  }
  build0(0, 1, n + 1);
  build(0, 1, n + 1);
  build1(0, 1, n + 1);
  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    if (a[i] == 1) {
      ans++;
      int mx = 1;
      for (int j = i + 1; j <= n; ++j) {
        if (a[j] == 1) break;
        mx = max(mx, a[j]);
        if (mx <= j) {
          ans += f(j - mx + 1, j, mx);
        }
      }
    }
  }
  for (int i = n; i >= 1; --i) {
    if (a[i] == 1) {
      int mx = 1;
      for (int j = i - 1; j >= 1; --j) {
        if (a[j] == 1) break;
        mx = max(mx, a[j]);
        if (n - j + 1 >= mx) {
          ans += f(j, j + mx - 1, mx);
        }
      }
    }
  }
  cout << ans << '\n';
  return 0;
}

```