#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = 1000000000;
const ll LINF = 1001002003004005006ll;
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
struct IOSetup {
  IOSetup() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(12);
  }
} iosetup;
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << p.first << " " << p.second;
  return os;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < (int)v.size(); i++)
    os << v[i] << (i + 1 == (int)v.size() ? "" : " ");
  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 (T &x : v) is >> x;
  return is;
}
ll h[300010], hs[300011];
signed main() {
  mt19937 mt(time(0));
  for (int i = 1; i < 300010; i++) {
    h[i] = mt();
    hs[i] = (hs[i - 1] ^ h[i]);
  }
  int n;
  cin >> n;
  vector<int> a(n);
  cin >> a;
  int res = 0;
  for (int _ = 0; _ < (2); _++) {
    vector<ll> xs(n + 1);
    xs[0] = 0;
    for (int i = 0; i < (n); i++) xs[i + 1] = xs[i] ^ h[a[i]];
    for (int i = 0; i < (n); i++)
      if (a[i] == 1) {
        int mx = 0;
        for (int j = i + 1; j < n; j++) {
          if (a[j] == 1) break;
          chmax(mx, a[j]);
          if (j + 1 - mx >= 0 and ((xs[j + 1] ^ xs[j + 1 - mx]) == hs[mx])) {
            res++;
          }
        }
      }
    reverse(a.begin(), a.end());
  }
  for (int i = 0; i < (n); i++)
    if (a[i] == 1) res++;
  cout << res << endl;
  return 0;
}

### Prompt

Generate 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>
using namespace std;
using ll = long long;
const int INF = 1000000000;
const ll LINF = 1001002003004005006ll;
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
struct IOSetup {
  IOSetup() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(12);
  }
} iosetup;
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << p.first << " " << p.second;
  return os;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < (int)v.size(); i++)
    os << v[i] << (i + 1 == (int)v.size() ? "" : " ");
  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 (T &x : v) is >> x;
  return is;
}
ll h[300010], hs[300011];
signed main() {
  mt19937 mt(time(0));
  for (int i = 1; i < 300010; i++) {
    h[i] = mt();
    hs[i] = (hs[i - 1] ^ h[i]);
  }
  int n;
  cin >> n;
  vector<int> a(n);
  cin >> a;
  int res = 0;
  for (int _ = 0; _ < (2); _++) {
    vector<ll> xs(n + 1);
    xs[0] = 0;
    for (int i = 0; i < (n); i++) xs[i + 1] = xs[i] ^ h[a[i]];
    for (int i = 0; i < (n); i++)
      if (a[i] == 1) {
        int mx = 0;
        for (int j = i + 1; j < n; j++) {
          if (a[j] == 1) break;
          chmax(mx, a[j]);
          if (j + 1 - mx >= 0 and ((xs[j + 1] ^ xs[j + 1 - mx]) == hs[mx])) {
            res++;
          }
        }
      }
    reverse(a.begin(), a.end());
  }
  for (int i = 0; i < (n); i++)
    if (a[i] == 1) res++;
  cout << res << endl;
  return 0;
}

```