#include <bits/stdc++.h>
using namespace std;
template <class c>
struct rge {
  c b, e;
};
template <class c>
rge<c> range(c i, c j) {
  return rge<c>{i, j};
}
template <class c>
auto dud(c* x) -> decltype(cerr << *x, 0);
template <class c>
char dud(...);
struct debug {
  template <class c>
  debug& operator<<(const c&) {
    return *this;
  }
};
vector<char*> tokenizer(const char* args) {
  char* token = new char[111];
  strcpy(token, args);
  token = strtok(token, ", ");
  vector<char*> v({token});
  while (token = strtok(NULL, ", ")) v.push_back(token);
  return reverse(v.begin(), v.end()), v;
}
void debugg(vector<char*> args) { cerr << "\b\b "; }
template <typename Head, typename... Tail>
void debugg(vector<char*> args, Head H, Tail... T) {
  debug() << " [" << args.back() << ": " << H << "] ";
  args.pop_back();
  debugg(args, T...);
}
int64_t powm(int64_t a, int64_t b, int64_t mod) {
  int64_t res = 1;
  while (b) {
    if (b & 1) res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}
const long double pi = 3.14159265358979323846;
const int64_t mod = 1e9 + 7;
const int64_t N = 1000005;
const int64_t inf = 1e9;
const int64_t INF = 1e18;
mt19937_64 rnd(time(NULL));
pair<int64_t, int64_t> hsh[N], sumHsh[N], prefix[N];
pair<int64_t, int64_t> upd(pair<int64_t, int64_t> a, pair<int64_t, int64_t> b) {
  return make_pair(a.first ^ b.first, a.second ^ b.second);
}
void solve() {
  int64_t n;
  cin >> n;
  vector<int64_t> a(n + 1);
  for (int64_t i = 0; i < (n); i++) cin >> a[i + 1], a[i + 1]--;
  sumHsh[0] = make_pair(0, 0);
  for (int64_t i = 0; i <= n; i++) {
    hsh[i].first = rnd();
    hsh[i].second = rnd();
    sumHsh[i] = hsh[i];
    sumHsh[i] = upd(sumHsh[i], sumHsh[i - 1]);
  }
  int64_t ans = 0;
  for (int64_t ttt = 0; ttt < 2; ttt++) {
    for (int64_t i = 1; i <= n; i++) prefix[i] = upd(hsh[a[i]], prefix[i - 1]);
    for (int64_t i = 1; i <= n; i++) {
      if (!a[i]) {
        if (ttt) ans++;
        set<int64_t> pres;
        pres.insert(a[i]);
        int64_t r = i + 1;
        while (r <= n and pres.find(a[r]) == pres.end()) {
          pres.insert(a[r]);
          {};
          if (r - (*--pres.end()) - 1 >= 0 and
              upd(prefix[r - (*--pres.end()) - 1], prefix[r]) ==
                  sumHsh[*--pres.end()]) {
            ans++;
            {};
          }
          r++;
        }
      }
    }
    reverse(a.begin() + 1, a.end());
  }
  cout << ans << '\n';
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  cout << fixed << setprecision(15);
  int64_t tc = 1;
  for (int64_t t = 1; t <= tc; t++) {
    solve();
  }
  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;
template <class c>
struct rge {
  c b, e;
};
template <class c>
rge<c> range(c i, c j) {
  return rge<c>{i, j};
}
template <class c>
auto dud(c* x) -> decltype(cerr << *x, 0);
template <class c>
char dud(...);
struct debug {
  template <class c>
  debug& operator<<(const c&) {
    return *this;
  }
};
vector<char*> tokenizer(const char* args) {
  char* token = new char[111];
  strcpy(token, args);
  token = strtok(token, ", ");
  vector<char*> v({token});
  while (token = strtok(NULL, ", ")) v.push_back(token);
  return reverse(v.begin(), v.end()), v;
}
void debugg(vector<char*> args) { cerr << "\b\b "; }
template <typename Head, typename... Tail>
void debugg(vector<char*> args, Head H, Tail... T) {
  debug() << " [" << args.back() << ": " << H << "] ";
  args.pop_back();
  debugg(args, T...);
}
int64_t powm(int64_t a, int64_t b, int64_t mod) {
  int64_t res = 1;
  while (b) {
    if (b & 1) res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}
const long double pi = 3.14159265358979323846;
const int64_t mod = 1e9 + 7;
const int64_t N = 1000005;
const int64_t inf = 1e9;
const int64_t INF = 1e18;
mt19937_64 rnd(time(NULL));
pair<int64_t, int64_t> hsh[N], sumHsh[N], prefix[N];
pair<int64_t, int64_t> upd(pair<int64_t, int64_t> a, pair<int64_t, int64_t> b) {
  return make_pair(a.first ^ b.first, a.second ^ b.second);
}
void solve() {
  int64_t n;
  cin >> n;
  vector<int64_t> a(n + 1);
  for (int64_t i = 0; i < (n); i++) cin >> a[i + 1], a[i + 1]--;
  sumHsh[0] = make_pair(0, 0);
  for (int64_t i = 0; i <= n; i++) {
    hsh[i].first = rnd();
    hsh[i].second = rnd();
    sumHsh[i] = hsh[i];
    sumHsh[i] = upd(sumHsh[i], sumHsh[i - 1]);
  }
  int64_t ans = 0;
  for (int64_t ttt = 0; ttt < 2; ttt++) {
    for (int64_t i = 1; i <= n; i++) prefix[i] = upd(hsh[a[i]], prefix[i - 1]);
    for (int64_t i = 1; i <= n; i++) {
      if (!a[i]) {
        if (ttt) ans++;
        set<int64_t> pres;
        pres.insert(a[i]);
        int64_t r = i + 1;
        while (r <= n and pres.find(a[r]) == pres.end()) {
          pres.insert(a[r]);
          {};
          if (r - (*--pres.end()) - 1 >= 0 and
              upd(prefix[r - (*--pres.end()) - 1], prefix[r]) ==
                  sumHsh[*--pres.end()]) {
            ans++;
            {};
          }
          r++;
        }
      }
    }
    reverse(a.begin() + 1, a.end());
  }
  cout << ans << '\n';
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  cout << fixed << setprecision(15);
  int64_t tc = 1;
  for (int64_t t = 1; t <= tc; t++) {
    solve();
  }
  return 0;
}

```