#include <bits/stdc++.h>
using namespace std;
namespace fastExpress {
template <typename T>
inline T read() {
  T f = 1, x = 0;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * f;
}
template <typename T>
inline void write(T x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
}
template <typename T>
inline void print_char(T x, char ch) {
  write(x), putchar(ch);
}
template <typename T>
inline void char_print(char ch, T x) {
  putchar(ch);
  write(x);
}
template <typename T>
inline void print_space(T x) {
  print_char(x, ' ');
}
template <typename T>
inline void space_print(T x) {
  char_print(' ', x);
}
template <typename T>
inline void println(T x) {
  print_char(x, '\n');
}
template <typename T>
inline void lnprint(T x) {
  char_print('\n', x);
}
template <typename T>
inline void print_array_in_range1(T* a, int l, int r) {
  for (int(i) = (l); (i) <= (r); ++(i)) print_char(a[i], " \n"[i == r]);
}
template <typename T>
inline void print_array_in_range2(T* a, int l, int r) {
  println(r - l + 1), print_array_in_range1(a, l, r);
}
template <typename T>
inline void print_array_in_range3(T* a, int l, int r) {
  for (int(i) = (r); (i) >= (l); --(i)) print_char(a[i], " \n"[i == l]);
}
template <typename T>
inline void print_array_in_range4(T* a, int l, int r) {
  println(r - l + 1), print_array_in_range3(a, l, r);
}
template <typename T>
inline void print_array_in_range5(T* a, int l, int r) {
  for (int(i) = (l); (i) <= (r); ++(i)) println(a[i]);
}
template <typename T>
inline void print_array_in_range6(T* a, int l, int r) {
  println(r - l + 1), print_array_in_range5(a, l, r);
}
template <typename T>
inline void print_array_in_range7(T* a, int l, int r) {
  for (int(i) = (r); (i) >= (l); --(i)) println(a[i]);
}
template <typename T>
inline void print_array_in_range8(T* a, int l, int r) {
  println(r - l + 1), print_array_in_range7(a, l, r);
}
template <typename T>
inline void print_array_in_range9(T* a, int l, int r) {
  print_space(r - l + 1), print_array_in_range1(a, l, r);
}
template <typename T>
inline void print_array_in_range10(T* a, int l, int r) {
  print_space(r - l + 1), print_array_in_range3(a, l, r);
}
template <typename T>
inline void print_array_in_range11(T* a, int l, int r) {
  print_space(r - l + 1), print_array_in_range5(a, l, r);
}
template <typename T>
inline void print_array_in_range12(T* a, int l, int r) {
  print_space(r - l + 1), print_array_in_range7(a, l, r);
}
template <typename T>
inline void print_array_in_range13(T* a, int l, int r, int n) {
  println(n), print_array_in_range1(a, l, r);
}
template <typename T>
inline void print_array_in_range14(T* a, int l, int r, int n) {
  println(n), print_array_in_range1(a, l, r);
}
template <typename T>
inline void print_array_in_range15(T* a, int l, int r, int n) {
  println(n), print_array_in_range1(a, l, r);
}
template <typename T>
inline void print_array_in_range16(T* a, int l, int r, int n) {
  println(n), print_array_in_range1(a, l, r);
}
template <typename T>
inline void print_array_in_range17(T* a, int l, int r, int n) {
  print_space(n), print_array_in_range1(a, l, r);
}
template <typename T>
inline void print_array_in_range18(T* a, int l, int r, int n) {
  print_space(n), print_array_in_range1(a, l, r);
}
template <typename T>
inline void print_array_in_range19(T* a, int l, int r, int n) {
  print_space(n), print_array_in_range1(a, l, r);
}
template <typename T>
inline void print_array_in_range20(T* a, int l, int r, int n) {
  print_space(n), print_array_in_range1(a, l, r);
}
template <typename T>
inline void print_array1(T* a, int n) {
  print_array_in_range1(a, 1, n);
}
template <typename T>
inline void print_array2(T* a, int n) {
  println(n), print_array1(a, n);
}
template <typename T>
inline void print_array3(T* a, int n) {
  print_array_in_range3(a, 1, n);
}
template <typename T>
inline void print_array4(T* a, int n) {
  println(n), print_array3(a, n);
}
template <typename T>
inline void print_array5(T* a, int n) {
  print_array_in_range5(a, 1, n);
}
template <typename T>
inline void print_array6(T* a, int n) {
  println(n), print_array5(a, n);
}
template <typename T>
inline void print_array7(T* a, int n) {
  print_array_in_range7(a, 1, n);
}
template <typename T>
inline void print_array8(T* a, int n) {
  println(n), print_array7(a, n);
}
template <typename T>
inline void print_array9(T* a, int n) {
  print_space(n), print_array1(a, n);
}
template <typename T>
inline void print_array10(T* a, int n) {
  print_space(n), print_array3(a, n);
}
template <typename T>
inline void print_array11(T* a, int n) {
  print_space(n), print_array5(a, n);
}
template <typename T>
inline void print_array12(T* a, int n) {
  print_space(n), print_array7(a, n);
}
template <typename T>
inline void vector_print(vector<T> a, int l, int r) {
  for (int(i) = (l); (i) <= (r); ++(i)) print_char(a[i], " \n"[i == r]);
}
template <typename T>
inline void map_print(map<T, T> a, int l, int r) {
  for (int(i) = (l); (i) <= (r); ++(i)) print_char(a[i], " \n"[i == r]);
}
template <typename T>
inline void stack_print(stack<T> a, char ch) {
  while (!a.empty()) print_char(a.top(), ch), a.pop();
}
template <typename T>
inline void queue_print(queue<T> a, char ch) {
  while (!a.empty()) print_char(a.front(), ch), a.pop();
}
template <typename T>
inline void pq_print(priority_queue<T> a, char ch) {
  while (!a.empty()) print_char(a.top(), ch), a.pop();
}
inline void print_string_in_range(string s, int l, int r) {
  for (int(i) = (l); (i) <= (r); ++(i)) putchar(s[i]);
}
inline void print_chars_in_range(char* a, int l, int r) {
  for (int(i) = (l); (i) <= (r); ++(i)) putchar(a[i]);
}
inline void print_allstring(string s) {
  print_string_in_range(s, 0, (int)s.size() - 1);
}
inline void filein(string s) { freopen((s + ".in").c_str(), "r", stdin); }
inline void fileout(string s) { freopen((s + ".out").c_str(), "w", stdout); }
inline void file(string s) { filein(s), fileout(s); }
}  // namespace fastExpress
using namespace fastExpress;
const int N = 3e5 + 7;
int n, ans, a[N], pre[N], nxt[N], pos[N], f[N], g[N], st[N][20], lg2[N];
inline int cal(int l, int r) {
  int len = lg2[r - l + 1];
  if (a[st[l][len]] >= a[st[r - (1 << len) + 1][len]]) return st[l][len];
  return st[r - (1 << len) + 1][len];
}
inline void solve(int l, int r) {
  if (l > r) return;
  if (l == r) return ans += (a[l] == 1), void();
  int mid = cal(l, r);
  if (mid - l < r - mid) {
    for (int(i) = (l); (i) <= (mid); ++(i)) {
      int t = i + a[mid] - 1;
      if (t < mid || t > r || t >= g[i]) continue;
      ans++;
    }
  } else {
    for (int(i) = (mid); (i) <= (r); ++(i)) {
      int t = i - a[mid] + 1;
      if (t < l || t > mid || t <= f[i]) continue;
      ans++;
    }
  }
  solve(l, mid - 1), solve(mid + 1, r);
}
int main() {
  n = read<int>();
  for (int(i) = (1); (i) <= (n); ++(i))
    a[i] = read<int>(), pre[i] = pos[a[i]], pos[a[i]] = i,
    f[i] = max(f[i - 1], pre[i]);
  for (int(i) = (1); (i) <= (n); ++(i)) pos[i] = n + 1, st[i][0] = i;
  g[n + 1] = n + 1;
  for (int(i) = (n); (i) >= (1); --(i))
    nxt[i] = pos[a[i]], pos[a[i]] = i, g[i] = min(g[i + 1], nxt[i]);
  for (int(i) = (2); (i) <= (n); ++(i)) lg2[i] = lg2[i >> 1] + 1;
  for (int(j) = (1); (j) <= (lg2[n]); ++(j))
    for (int(i) = (1); (i) <= (n - (1 << j) + 1); ++(i))
      if (a[st[i][j - 1]] >= a[st[i + (1 << j - 1)][j - 1]])
        st[i][j] = st[i][j - 1];
      else
        st[i][j] = st[i + (1 << j - 1)][j - 1];
  solve(1, n), write(ans);
  return 0;
}

### Prompt

Develop a solution in CPP to the problem described below:

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;
namespace fastExpress {
template <typename T>
inline T read() {
  T f = 1, x = 0;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * f;
}
template <typename T>
inline void write(T x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
}
template <typename T>
inline void print_char(T x, char ch) {
  write(x), putchar(ch);
}
template <typename T>
inline void char_print(char ch, T x) {
  putchar(ch);
  write(x);
}
template <typename T>
inline void print_space(T x) {
  print_char(x, ' ');
}
template <typename T>
inline void space_print(T x) {
  char_print(' ', x);
}
template <typename T>
inline void println(T x) {
  print_char(x, '\n');
}
template <typename T>
inline void lnprint(T x) {
  char_print('\n', x);
}
template <typename T>
inline void print_array_in_range1(T* a, int l, int r) {
  for (int(i) = (l); (i) <= (r); ++(i)) print_char(a[i], " \n"[i == r]);
}
template <typename T>
inline void print_array_in_range2(T* a, int l, int r) {
  println(r - l + 1), print_array_in_range1(a, l, r);
}
template <typename T>
inline void print_array_in_range3(T* a, int l, int r) {
  for (int(i) = (r); (i) >= (l); --(i)) print_char(a[i], " \n"[i == l]);
}
template <typename T>
inline void print_array_in_range4(T* a, int l, int r) {
  println(r - l + 1), print_array_in_range3(a, l, r);
}
template <typename T>
inline void print_array_in_range5(T* a, int l, int r) {
  for (int(i) = (l); (i) <= (r); ++(i)) println(a[i]);
}
template <typename T>
inline void print_array_in_range6(T* a, int l, int r) {
  println(r - l + 1), print_array_in_range5(a, l, r);
}
template <typename T>
inline void print_array_in_range7(T* a, int l, int r) {
  for (int(i) = (r); (i) >= (l); --(i)) println(a[i]);
}
template <typename T>
inline void print_array_in_range8(T* a, int l, int r) {
  println(r - l + 1), print_array_in_range7(a, l, r);
}
template <typename T>
inline void print_array_in_range9(T* a, int l, int r) {
  print_space(r - l + 1), print_array_in_range1(a, l, r);
}
template <typename T>
inline void print_array_in_range10(T* a, int l, int r) {
  print_space(r - l + 1), print_array_in_range3(a, l, r);
}
template <typename T>
inline void print_array_in_range11(T* a, int l, int r) {
  print_space(r - l + 1), print_array_in_range5(a, l, r);
}
template <typename T>
inline void print_array_in_range12(T* a, int l, int r) {
  print_space(r - l + 1), print_array_in_range7(a, l, r);
}
template <typename T>
inline void print_array_in_range13(T* a, int l, int r, int n) {
  println(n), print_array_in_range1(a, l, r);
}
template <typename T>
inline void print_array_in_range14(T* a, int l, int r, int n) {
  println(n), print_array_in_range1(a, l, r);
}
template <typename T>
inline void print_array_in_range15(T* a, int l, int r, int n) {
  println(n), print_array_in_range1(a, l, r);
}
template <typename T>
inline void print_array_in_range16(T* a, int l, int r, int n) {
  println(n), print_array_in_range1(a, l, r);
}
template <typename T>
inline void print_array_in_range17(T* a, int l, int r, int n) {
  print_space(n), print_array_in_range1(a, l, r);
}
template <typename T>
inline void print_array_in_range18(T* a, int l, int r, int n) {
  print_space(n), print_array_in_range1(a, l, r);
}
template <typename T>
inline void print_array_in_range19(T* a, int l, int r, int n) {
  print_space(n), print_array_in_range1(a, l, r);
}
template <typename T>
inline void print_array_in_range20(T* a, int l, int r, int n) {
  print_space(n), print_array_in_range1(a, l, r);
}
template <typename T>
inline void print_array1(T* a, int n) {
  print_array_in_range1(a, 1, n);
}
template <typename T>
inline void print_array2(T* a, int n) {
  println(n), print_array1(a, n);
}
template <typename T>
inline void print_array3(T* a, int n) {
  print_array_in_range3(a, 1, n);
}
template <typename T>
inline void print_array4(T* a, int n) {
  println(n), print_array3(a, n);
}
template <typename T>
inline void print_array5(T* a, int n) {
  print_array_in_range5(a, 1, n);
}
template <typename T>
inline void print_array6(T* a, int n) {
  println(n), print_array5(a, n);
}
template <typename T>
inline void print_array7(T* a, int n) {
  print_array_in_range7(a, 1, n);
}
template <typename T>
inline void print_array8(T* a, int n) {
  println(n), print_array7(a, n);
}
template <typename T>
inline void print_array9(T* a, int n) {
  print_space(n), print_array1(a, n);
}
template <typename T>
inline void print_array10(T* a, int n) {
  print_space(n), print_array3(a, n);
}
template <typename T>
inline void print_array11(T* a, int n) {
  print_space(n), print_array5(a, n);
}
template <typename T>
inline void print_array12(T* a, int n) {
  print_space(n), print_array7(a, n);
}
template <typename T>
inline void vector_print(vector<T> a, int l, int r) {
  for (int(i) = (l); (i) <= (r); ++(i)) print_char(a[i], " \n"[i == r]);
}
template <typename T>
inline void map_print(map<T, T> a, int l, int r) {
  for (int(i) = (l); (i) <= (r); ++(i)) print_char(a[i], " \n"[i == r]);
}
template <typename T>
inline void stack_print(stack<T> a, char ch) {
  while (!a.empty()) print_char(a.top(), ch), a.pop();
}
template <typename T>
inline void queue_print(queue<T> a, char ch) {
  while (!a.empty()) print_char(a.front(), ch), a.pop();
}
template <typename T>
inline void pq_print(priority_queue<T> a, char ch) {
  while (!a.empty()) print_char(a.top(), ch), a.pop();
}
inline void print_string_in_range(string s, int l, int r) {
  for (int(i) = (l); (i) <= (r); ++(i)) putchar(s[i]);
}
inline void print_chars_in_range(char* a, int l, int r) {
  for (int(i) = (l); (i) <= (r); ++(i)) putchar(a[i]);
}
inline void print_allstring(string s) {
  print_string_in_range(s, 0, (int)s.size() - 1);
}
inline void filein(string s) { freopen((s + ".in").c_str(), "r", stdin); }
inline void fileout(string s) { freopen((s + ".out").c_str(), "w", stdout); }
inline void file(string s) { filein(s), fileout(s); }
}  // namespace fastExpress
using namespace fastExpress;
const int N = 3e5 + 7;
int n, ans, a[N], pre[N], nxt[N], pos[N], f[N], g[N], st[N][20], lg2[N];
inline int cal(int l, int r) {
  int len = lg2[r - l + 1];
  if (a[st[l][len]] >= a[st[r - (1 << len) + 1][len]]) return st[l][len];
  return st[r - (1 << len) + 1][len];
}
inline void solve(int l, int r) {
  if (l > r) return;
  if (l == r) return ans += (a[l] == 1), void();
  int mid = cal(l, r);
  if (mid - l < r - mid) {
    for (int(i) = (l); (i) <= (mid); ++(i)) {
      int t = i + a[mid] - 1;
      if (t < mid || t > r || t >= g[i]) continue;
      ans++;
    }
  } else {
    for (int(i) = (mid); (i) <= (r); ++(i)) {
      int t = i - a[mid] + 1;
      if (t < l || t > mid || t <= f[i]) continue;
      ans++;
    }
  }
  solve(l, mid - 1), solve(mid + 1, r);
}
int main() {
  n = read<int>();
  for (int(i) = (1); (i) <= (n); ++(i))
    a[i] = read<int>(), pre[i] = pos[a[i]], pos[a[i]] = i,
    f[i] = max(f[i - 1], pre[i]);
  for (int(i) = (1); (i) <= (n); ++(i)) pos[i] = n + 1, st[i][0] = i;
  g[n + 1] = n + 1;
  for (int(i) = (n); (i) >= (1); --(i))
    nxt[i] = pos[a[i]], pos[a[i]] = i, g[i] = min(g[i + 1], nxt[i]);
  for (int(i) = (2); (i) <= (n); ++(i)) lg2[i] = lg2[i >> 1] + 1;
  for (int(j) = (1); (j) <= (lg2[n]); ++(j))
    for (int(i) = (1); (i) <= (n - (1 << j) + 1); ++(i))
      if (a[st[i][j - 1]] >= a[st[i + (1 << j - 1)][j - 1]])
        st[i][j] = st[i][j - 1];
      else
        st[i][j] = st[i + (1 << j - 1)][j - 1];
  solve(1, n), write(ans);
  return 0;
}

```