#include <bits/stdc++.h>
using namespace std;
int n, m, k, x, y, z;
int b[300500], c[300500];
pair<int, int> a[300500];
int primes[1005000];
int Abs(int x) { return (x > 0 ? x : -x); }
int binpow(int x, int y) {
  if (y == 0) {
    return 1;
  }
  int tmp = binpow(x, y / 2);
  tmp = tmp * tmp % 1000000007;
  if (y % 2) {
    return x * tmp % 1000000007;
  }
  return tmp;
}
void resheto() {
  for (int i = 2; i <= 1000000; i++) {
    primes[i] = 1;
  }
  for (int i = 2; i <= 1000000; i++) {
    if (primes[i]) {
      for (int j = i * 2; j <= 1000000; j += i) {
        primes[j] = 0;
      }
    }
  }
}
vector<int> g[300500];
vector<pair<int, int> > f[2], h;
multiset<int> gh;
int mins[300500];
void pshe(int x, pair<int, int> y) {
  if (mins[y.first] > y.second) {
    f[x].push_back(y);
  }
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    scanf("%d", &x);
    g[x].push_back(i);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j + 1 < g[i].size(); j++) {
      h.push_back(make_pair(g[i][j], g[i][j + 1]));
    }
  }
  sort(h.begin(), h.end());
  for (auto to : h) {
    gh.insert(to.second);
  }
  int cur = 0;
  for (auto x : g[1]) {
    f[cur].push_back(make_pair(x, x));
  }
  int pos = 0;
  for (int i = 0; i < n; i++) {
    if (gh.empty()) {
      mins[i] = n + 1;
    } else {
      mins[i] = *(gh.begin());
    }
    while (pos < h.size() && h[pos].first == i) {
      gh.erase(gh.find(h[pos].second));
      pos++;
    }
  }
  int ans = 0;
  for (int i = 2; i <= n + 1; i++) {
    int nxt = (cur ^ 1);
    f[nxt].clear();
    const auto& vec = g[i];
    for (auto p : f[cur]) {
      int l = p.first, r = p.second;
      if (r - l + 1 == i - 1) {
        ans++;
      }
      int it = upper_bound(vec.begin(), vec.end(), l) - vec.begin();
      if (it == vec.size()) {
        if (it != 0) {
          pshe(nxt, make_pair(vec[it - 1], r));
        }
      } else if (vec[it] < r) {
        if (it + 2 < vec.size() && vec[it + 1] < r) {
          continue;
        }
        f[nxt].push_back(make_pair(l, r));
      } else {
        if (it != vec.size()) {
          pshe(nxt, make_pair(l, vec[it]));
        }
        if (it != 0) {
          pshe(nxt, make_pair(vec[it - 1], r));
        }
      }
    }
    cur ^= 1;
  }
  cout << ans << endl;
  return 0;
}

### Prompt

Construct a CPP code solution to the problem outlined:

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, m, k, x, y, z;
int b[300500], c[300500];
pair<int, int> a[300500];
int primes[1005000];
int Abs(int x) { return (x > 0 ? x : -x); }
int binpow(int x, int y) {
  if (y == 0) {
    return 1;
  }
  int tmp = binpow(x, y / 2);
  tmp = tmp * tmp % 1000000007;
  if (y % 2) {
    return x * tmp % 1000000007;
  }
  return tmp;
}
void resheto() {
  for (int i = 2; i <= 1000000; i++) {
    primes[i] = 1;
  }
  for (int i = 2; i <= 1000000; i++) {
    if (primes[i]) {
      for (int j = i * 2; j <= 1000000; j += i) {
        primes[j] = 0;
      }
    }
  }
}
vector<int> g[300500];
vector<pair<int, int> > f[2], h;
multiset<int> gh;
int mins[300500];
void pshe(int x, pair<int, int> y) {
  if (mins[y.first] > y.second) {
    f[x].push_back(y);
  }
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    scanf("%d", &x);
    g[x].push_back(i);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j + 1 < g[i].size(); j++) {
      h.push_back(make_pair(g[i][j], g[i][j + 1]));
    }
  }
  sort(h.begin(), h.end());
  for (auto to : h) {
    gh.insert(to.second);
  }
  int cur = 0;
  for (auto x : g[1]) {
    f[cur].push_back(make_pair(x, x));
  }
  int pos = 0;
  for (int i = 0; i < n; i++) {
    if (gh.empty()) {
      mins[i] = n + 1;
    } else {
      mins[i] = *(gh.begin());
    }
    while (pos < h.size() && h[pos].first == i) {
      gh.erase(gh.find(h[pos].second));
      pos++;
    }
  }
  int ans = 0;
  for (int i = 2; i <= n + 1; i++) {
    int nxt = (cur ^ 1);
    f[nxt].clear();
    const auto& vec = g[i];
    for (auto p : f[cur]) {
      int l = p.first, r = p.second;
      if (r - l + 1 == i - 1) {
        ans++;
      }
      int it = upper_bound(vec.begin(), vec.end(), l) - vec.begin();
      if (it == vec.size()) {
        if (it != 0) {
          pshe(nxt, make_pair(vec[it - 1], r));
        }
      } else if (vec[it] < r) {
        if (it + 2 < vec.size() && vec[it + 1] < r) {
          continue;
        }
        f[nxt].push_back(make_pair(l, r));
      } else {
        if (it != vec.size()) {
          pshe(nxt, make_pair(l, vec[it]));
        }
        if (it != 0) {
          pshe(nxt, make_pair(vec[it - 1], r));
        }
      }
    }
    cur ^= 1;
  }
  cout << ans << endl;
  return 0;
}

```