#include <bits/stdc++.h>
using namespace std;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
clock_t START;
inline long double TIME() {
  return (long double)(clock() - START) / CLOCKS_PER_SEC;
}
void SHOW() {
  cout << fixed << setprecision(10);
  cout << TIME() << " SECONDS FROM START\n";
}
long double TL = 2.0;
long long calls = 0;
inline bool IS() {
  if (++calls == 1000) {
    calls = 0;
    if (TL - TIME() < 0.1) return true;
  }
  return false;
}
template <typename T1, typename T2>
inline void amin(T1 &a, T2 b) {
  if (a > b) a = b;
}
template <typename T1, typename T2>
inline void amax(T1 &a, T2 b) {
  if (a < b) a = b;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, pair<T1, T2> &p) {
  os << p.first << ' ' << p.second << endl;
  return os;
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
  is >> p.first >> p.second;
  return is;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
  for (auto &u : v) is >> u;
  return is;
}
template <typename T>
ostream &operator<<(ostream &os, vector<T> &v) {
  for (auto &u : v) os << u << ' ';
  os << endl;
  return os;
}
long long hash_mod[4] = {1000000007, 998244353, 1000000009, 999999937},
          mod = hash_mod[rnd() % 4];
long long hash_pows[4] = {179, 239, 1007, 2003}, P = hash_pows[rnd() % 4];
long long binpow(long long first, long long p) {
  long long res = 1;
  while (p) {
    if (p % 2) res = (res * first) % mod;
    first = (first * first) % mod;
    p /= 2;
  }
  return res;
}
const long long N = 1e6 + 7, NS = 2e3 + 7, lg = 20, sq = 550, inf = 2e9 + 7,
                SM = 1e2 + 7;
const long double eps = 1e-9;
long long n, ans = 0;
long long a[N], p[N], need[N];
void norm(long long &first) {
  while (first >= mod) first -= mod;
  while (first < 0) first += mod;
}
long long sum(long long l, long long r) {
  long long res = p[r];
  if (l) res -= p[l - 1];
  norm(res);
  return res;
}
long long check(long long l, long long r) {
  long long len = r - l + 1;
  if (r < l || l < 0 || r >= n) {
    return 0;
  }
  return need[len - 1] == sum(l, r);
}
void solve() {
  cin >> n;
  for (long long i = 0; i < n; i++) {
    long long first;
    cin >> first;
    a[i] = first;
    p[i] = (binpow(P, first));
    need[i] = (binpow(P, i + 1));
  }
  for (long long i = 1; i < n; i++) {
    p[i] += p[i - 1];
    need[i] += need[i - 1];
    norm(p[i]);
    norm(need[i]);
  }
  for (long long i = 0; i < n; i++) {
    if (a[i] == 1) {
      map<long long, bool> mp;
      long long r = i, mx = 1;
      while (r < n) {
        if (mp.count(a[r])) break;
        mp[a[r]] = 1;
        amax(mx, a[r]);
        ans += check(r - mx + 1, r);
        r++;
      }
    }
  }
  for (long long i = n - 1; i >= 0; i--) {
    if (a[i] == 1) {
      map<long long, bool> mp;
      long long l = i, mx = 1;
      while (l >= 0) {
        if (mp.count(a[l])) break;
        mp[a[l]] = 1;
        amax(mx, a[l]);
        if (mx != 1) ans += check(l, l + mx - 1);
        l--;
      }
    }
  }
  cout << ans;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  solve();
}

### Prompt

Please create a solution in Cpp 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;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
clock_t START;
inline long double TIME() {
  return (long double)(clock() - START) / CLOCKS_PER_SEC;
}
void SHOW() {
  cout << fixed << setprecision(10);
  cout << TIME() << " SECONDS FROM START\n";
}
long double TL = 2.0;
long long calls = 0;
inline bool IS() {
  if (++calls == 1000) {
    calls = 0;
    if (TL - TIME() < 0.1) return true;
  }
  return false;
}
template <typename T1, typename T2>
inline void amin(T1 &a, T2 b) {
  if (a > b) a = b;
}
template <typename T1, typename T2>
inline void amax(T1 &a, T2 b) {
  if (a < b) a = b;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, pair<T1, T2> &p) {
  os << p.first << ' ' << p.second << endl;
  return os;
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
  is >> p.first >> p.second;
  return is;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
  for (auto &u : v) is >> u;
  return is;
}
template <typename T>
ostream &operator<<(ostream &os, vector<T> &v) {
  for (auto &u : v) os << u << ' ';
  os << endl;
  return os;
}
long long hash_mod[4] = {1000000007, 998244353, 1000000009, 999999937},
          mod = hash_mod[rnd() % 4];
long long hash_pows[4] = {179, 239, 1007, 2003}, P = hash_pows[rnd() % 4];
long long binpow(long long first, long long p) {
  long long res = 1;
  while (p) {
    if (p % 2) res = (res * first) % mod;
    first = (first * first) % mod;
    p /= 2;
  }
  return res;
}
const long long N = 1e6 + 7, NS = 2e3 + 7, lg = 20, sq = 550, inf = 2e9 + 7,
                SM = 1e2 + 7;
const long double eps = 1e-9;
long long n, ans = 0;
long long a[N], p[N], need[N];
void norm(long long &first) {
  while (first >= mod) first -= mod;
  while (first < 0) first += mod;
}
long long sum(long long l, long long r) {
  long long res = p[r];
  if (l) res -= p[l - 1];
  norm(res);
  return res;
}
long long check(long long l, long long r) {
  long long len = r - l + 1;
  if (r < l || l < 0 || r >= n) {
    return 0;
  }
  return need[len - 1] == sum(l, r);
}
void solve() {
  cin >> n;
  for (long long i = 0; i < n; i++) {
    long long first;
    cin >> first;
    a[i] = first;
    p[i] = (binpow(P, first));
    need[i] = (binpow(P, i + 1));
  }
  for (long long i = 1; i < n; i++) {
    p[i] += p[i - 1];
    need[i] += need[i - 1];
    norm(p[i]);
    norm(need[i]);
  }
  for (long long i = 0; i < n; i++) {
    if (a[i] == 1) {
      map<long long, bool> mp;
      long long r = i, mx = 1;
      while (r < n) {
        if (mp.count(a[r])) break;
        mp[a[r]] = 1;
        amax(mx, a[r]);
        ans += check(r - mx + 1, r);
        r++;
      }
    }
  }
  for (long long i = n - 1; i >= 0; i--) {
    if (a[i] == 1) {
      map<long long, bool> mp;
      long long l = i, mx = 1;
      while (l >= 0) {
        if (mp.count(a[l])) break;
        mp[a[l]] = 1;
        amax(mx, a[l]);
        if (mx != 1) ans += check(l, l + mx - 1);
        l--;
      }
    }
  }
  cout << ans;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  solve();
}

```