#include <bits/stdc++.h>
int32_t* data;
std::vector<int32_t>* segtree;
void build(int32_t node, int32_t left, int32_t right) {
  if (right - left == 1) {
    segtree[node] = {data[left]};
    return;
  }
  int32_t m = (left + right) / 2;
  build(node * 2 + 1, left, m);
  build(node * 2 + 2, m, right);
  segtree[node].resize(right - left);
  std::merge(segtree[node * 2 + 1].begin(), segtree[node * 2 + 1].end(),
             segtree[node * 2 + 2].begin(), segtree[node * 2 + 2].end(),
             segtree[node].begin());
}
int32_t query(int32_t node, int32_t left, int32_t right, int32_t query_left,
              int32_t query_right, int32_t param) {
  if (right <= query_left || left >= query_right) return 0;
  if (left >= query_left && right <= query_right) {
    return right - left -
           (std::lower_bound(segtree[node].begin(), segtree[node].end(),
                             param) -
            segtree[node].begin());
  }
  int32_t m = (left + right) / 2;
  return query(node * 2 + 1, left, m, query_left, query_right, param) +
         query(node * 2 + 2, m, right, query_left, query_right, param);
}
struct SparseTable {
  int32_t** table;
  bool min_;
  SparseTable(int32_t* data, int32_t n, bool min) {
    min_ = min;
    int32_t max_level = 0;
    while ((1 << max_level) <= n) max_level++;
    max_level--;
    table = new int32_t*[max_level + 1];
    for (int32_t i = 0; i <= max_level; i++) {
      int32_t length = n - (1 << i) + 1;
      table[i] = new int32_t[length];
      for (int32_t j = 0; j < length; j++)
        if (i == 0)
          table[i][j] = data[j];
        else
          table[i][j] =
              min_ ? std::min(table[i - 1][j], table[i - 1][j + (1 << i) / 2])
                   : std::max(table[i - 1][j], table[i - 1][j + (1 << i) / 2]);
    }
  }
  int32_t query(int32_t left, int32_t right) {
    int32_t level = 0;
    while ((1 << level) <= right - left + 1) level++;
    level--;
    return min_ ? std::min(table[level][left],
                           table[level][right - (1 << level) + 1])
                : std::max(table[level][left],
                           table[level][right - (1 << level) + 1]);
  }
};
int main() {
  int32_t n;
  std::cin >> n;
  int32_t* arr = new int32_t[n];
  for (int32_t i = 0; i < n; i++) std::cin >> arr[i];
  std::map<int32_t, int32_t> last;
  int32_t* next = new int32_t[n];
  for (int32_t i = 0; i < n; i++) next[i] = INT32_MAX;
  for (int32_t i = 0; i < n; i++) {
    auto it = last.find(arr[i]);
    if (it != last.end()) next[it->second] = i;
    last[arr[i]] = i;
  }
  std::set<std::pair<int32_t, int32_t> > unmatched;
  int32_t* prev_bigger = new int32_t[n];
  for (int32_t i = 0; i < n; i++) prev_bigger[i] = INT32_MIN;
  for (int32_t i = n - 1; i >= 0; i--) {
    while (unmatched.size() > 0 && unmatched.begin()->first < arr[i]) {
      prev_bigger[unmatched.begin()->second] = i;
      unmatched.erase(unmatched.begin());
    }
    unmatched.emplace(arr[i], i);
  }
  unmatched.clear();
  int32_t* next_bigger = new int32_t[n];
  for (int32_t i = 0; i < n; i++) next_bigger[i] = INT32_MAX;
  for (int32_t i = 0; i < n; i++) {
    while (unmatched.size() > 0 && unmatched.begin()->first < arr[i]) {
      next_bigger[unmatched.begin()->second] = i;
      unmatched.erase(unmatched.begin());
    }
    unmatched.emplace(arr[i], i);
  }
  SparseTable table1(next, n, true);
  int32_t* num_unique = new int32_t[n];
  for (int32_t i = 0; i < n; i++) {
    int32_t left = 1, right = n - i + 1;
    while (right - left > 1) {
      int32_t m = (left + right) / 2;
      if (table1.query(i, i + m - 1) > i + m - 1)
        left = m;
      else
        right = m;
    }
    num_unique[i] = left;
  }
  data = num_unique;
  segtree = new std::vector<int32_t>[n * 4];
  build(0, 0, n);
  int64_t answer = 0;
  for (int32_t i = 0; i < n; i++) {
    int32_t left = std::max(std::max(0, i - arr[i] + 1), prev_bigger[i] + 1);
    int32_t right = std::min(i + 1, next_bigger[i] - arr[i] + 1);
    if (right <= left) continue;
    answer += query(0, 0, n, left, right, arr[i]);
  }
  std::cout << answer;
  return 0;
}

### Prompt

Your task is to create 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>
int32_t* data;
std::vector<int32_t>* segtree;
void build(int32_t node, int32_t left, int32_t right) {
  if (right - left == 1) {
    segtree[node] = {data[left]};
    return;
  }
  int32_t m = (left + right) / 2;
  build(node * 2 + 1, left, m);
  build(node * 2 + 2, m, right);
  segtree[node].resize(right - left);
  std::merge(segtree[node * 2 + 1].begin(), segtree[node * 2 + 1].end(),
             segtree[node * 2 + 2].begin(), segtree[node * 2 + 2].end(),
             segtree[node].begin());
}
int32_t query(int32_t node, int32_t left, int32_t right, int32_t query_left,
              int32_t query_right, int32_t param) {
  if (right <= query_left || left >= query_right) return 0;
  if (left >= query_left && right <= query_right) {
    return right - left -
           (std::lower_bound(segtree[node].begin(), segtree[node].end(),
                             param) -
            segtree[node].begin());
  }
  int32_t m = (left + right) / 2;
  return query(node * 2 + 1, left, m, query_left, query_right, param) +
         query(node * 2 + 2, m, right, query_left, query_right, param);
}
struct SparseTable {
  int32_t** table;
  bool min_;
  SparseTable(int32_t* data, int32_t n, bool min) {
    min_ = min;
    int32_t max_level = 0;
    while ((1 << max_level) <= n) max_level++;
    max_level--;
    table = new int32_t*[max_level + 1];
    for (int32_t i = 0; i <= max_level; i++) {
      int32_t length = n - (1 << i) + 1;
      table[i] = new int32_t[length];
      for (int32_t j = 0; j < length; j++)
        if (i == 0)
          table[i][j] = data[j];
        else
          table[i][j] =
              min_ ? std::min(table[i - 1][j], table[i - 1][j + (1 << i) / 2])
                   : std::max(table[i - 1][j], table[i - 1][j + (1 << i) / 2]);
    }
  }
  int32_t query(int32_t left, int32_t right) {
    int32_t level = 0;
    while ((1 << level) <= right - left + 1) level++;
    level--;
    return min_ ? std::min(table[level][left],
                           table[level][right - (1 << level) + 1])
                : std::max(table[level][left],
                           table[level][right - (1 << level) + 1]);
  }
};
int main() {
  int32_t n;
  std::cin >> n;
  int32_t* arr = new int32_t[n];
  for (int32_t i = 0; i < n; i++) std::cin >> arr[i];
  std::map<int32_t, int32_t> last;
  int32_t* next = new int32_t[n];
  for (int32_t i = 0; i < n; i++) next[i] = INT32_MAX;
  for (int32_t i = 0; i < n; i++) {
    auto it = last.find(arr[i]);
    if (it != last.end()) next[it->second] = i;
    last[arr[i]] = i;
  }
  std::set<std::pair<int32_t, int32_t> > unmatched;
  int32_t* prev_bigger = new int32_t[n];
  for (int32_t i = 0; i < n; i++) prev_bigger[i] = INT32_MIN;
  for (int32_t i = n - 1; i >= 0; i--) {
    while (unmatched.size() > 0 && unmatched.begin()->first < arr[i]) {
      prev_bigger[unmatched.begin()->second] = i;
      unmatched.erase(unmatched.begin());
    }
    unmatched.emplace(arr[i], i);
  }
  unmatched.clear();
  int32_t* next_bigger = new int32_t[n];
  for (int32_t i = 0; i < n; i++) next_bigger[i] = INT32_MAX;
  for (int32_t i = 0; i < n; i++) {
    while (unmatched.size() > 0 && unmatched.begin()->first < arr[i]) {
      next_bigger[unmatched.begin()->second] = i;
      unmatched.erase(unmatched.begin());
    }
    unmatched.emplace(arr[i], i);
  }
  SparseTable table1(next, n, true);
  int32_t* num_unique = new int32_t[n];
  for (int32_t i = 0; i < n; i++) {
    int32_t left = 1, right = n - i + 1;
    while (right - left > 1) {
      int32_t m = (left + right) / 2;
      if (table1.query(i, i + m - 1) > i + m - 1)
        left = m;
      else
        right = m;
    }
    num_unique[i] = left;
  }
  data = num_unique;
  segtree = new std::vector<int32_t>[n * 4];
  build(0, 0, n);
  int64_t answer = 0;
  for (int32_t i = 0; i < n; i++) {
    int32_t left = std::max(std::max(0, i - arr[i] + 1), prev_bigger[i] + 1);
    int32_t right = std::min(i + 1, next_bigger[i] - arr[i] + 1);
    if (right <= left) continue;
    answer += query(0, 0, n, left, right, arr[i]);
  }
  std::cout << answer;
  return 0;
}

```