#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const long long INF = mod * mod;
const double eps = 1e-8;
const double pi = acos(-1.0);
struct SegT {
 private:
  int sz;
  vector<int> node;
  const int init_c = 1;

 public:
  SegT(int n) {
    sz = 1;
    while (sz < n) sz *= 2;
    node.resize(2 * sz - 1, init_c);
    for (int i = sz - 1 - 1; i >= 0; i--) {
      node[i] = f(node[2 * i + 1], node[2 * i + 2]);
    }
  }
  int f(int a, int b) { return a + b; }
  void update(int k, int a) {
    k += sz - 1;
    node[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      node[k] = f(node[k * 2 + 1], node[k * 2 + 2]);
    }
  }
  int query(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0) r = sz;
    if (r <= a || b <= l)
      return 0;
    else if (a <= l && r <= b)
      return node[k];
    else {
      int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
      int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
      return f(vl, vr);
    }
  }
};
const int mn = 300001;
vector<int> loc[mn];
vector<int> u[mn];
int rri[mn];
void solve() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    loc[a[i]].push_back(i);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < (int)loc[i].size() - 1; j++) {
      int l = loc[i][j], r = loc[i][j + 1];
      u[r - l + 1].push_back(l);
    }
  }
  long long ans = 0;
  queue<int> ban;
  vector<bool> used(n, false);
  SegT st(n);
  set<int> ss;
  for (int i = 0; i < n; i++) ss.insert(i);
  ss.insert(-1);
  ss.insert(n);
  for (int i = 1; i <= n; i++) {
    int len = ban.size();
    for (int j = 0; j < len; j++) {
      int cur = ban.front();
      ban.pop();
      if (!used[cur]) {
        used[cur] = true;
        st.update(cur, 0);
        if (cur - 1 >= 0 && !used[cur - 1]) ban.push(cur - 1);
      }
    }
    for (int j = 0; j < u[i].size(); j++) {
      if (!used[u[i][j]]) {
        int cur = u[i][j];
        used[cur] = true;
        st.update(cur, 0);
        if (cur - 1 >= 0 && !used[cur - 1]) ban.push(cur - 1);
      }
    }
    for (int j = 0; j < loc[i].size(); j++) {
      int id = loc[i][j];
      ss.erase(id);
    }
    map<int, bool> mp;
    for (int j = 0; j < loc[i].size(); j++) {
      int id = loc[i][j];
      ss.insert(id);
      auto itr = ss.find(id);
      itr--;
      int le = *itr;
      itr++;
      itr++;
      int ri = *itr;
      ss.erase(id);
      le = le + 1;
      ri = ri - i;
      if (mp[le]) continue;
      mp[le] = true;
      ans += st.query(le, ri + 1);
    }
  }
  cout << ans << endl;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  solve();
  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;
const long long mod = 1000000007;
const long long INF = mod * mod;
const double eps = 1e-8;
const double pi = acos(-1.0);
struct SegT {
 private:
  int sz;
  vector<int> node;
  const int init_c = 1;

 public:
  SegT(int n) {
    sz = 1;
    while (sz < n) sz *= 2;
    node.resize(2 * sz - 1, init_c);
    for (int i = sz - 1 - 1; i >= 0; i--) {
      node[i] = f(node[2 * i + 1], node[2 * i + 2]);
    }
  }
  int f(int a, int b) { return a + b; }
  void update(int k, int a) {
    k += sz - 1;
    node[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      node[k] = f(node[k * 2 + 1], node[k * 2 + 2]);
    }
  }
  int query(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0) r = sz;
    if (r <= a || b <= l)
      return 0;
    else if (a <= l && r <= b)
      return node[k];
    else {
      int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
      int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
      return f(vl, vr);
    }
  }
};
const int mn = 300001;
vector<int> loc[mn];
vector<int> u[mn];
int rri[mn];
void solve() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    loc[a[i]].push_back(i);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < (int)loc[i].size() - 1; j++) {
      int l = loc[i][j], r = loc[i][j + 1];
      u[r - l + 1].push_back(l);
    }
  }
  long long ans = 0;
  queue<int> ban;
  vector<bool> used(n, false);
  SegT st(n);
  set<int> ss;
  for (int i = 0; i < n; i++) ss.insert(i);
  ss.insert(-1);
  ss.insert(n);
  for (int i = 1; i <= n; i++) {
    int len = ban.size();
    for (int j = 0; j < len; j++) {
      int cur = ban.front();
      ban.pop();
      if (!used[cur]) {
        used[cur] = true;
        st.update(cur, 0);
        if (cur - 1 >= 0 && !used[cur - 1]) ban.push(cur - 1);
      }
    }
    for (int j = 0; j < u[i].size(); j++) {
      if (!used[u[i][j]]) {
        int cur = u[i][j];
        used[cur] = true;
        st.update(cur, 0);
        if (cur - 1 >= 0 && !used[cur - 1]) ban.push(cur - 1);
      }
    }
    for (int j = 0; j < loc[i].size(); j++) {
      int id = loc[i][j];
      ss.erase(id);
    }
    map<int, bool> mp;
    for (int j = 0; j < loc[i].size(); j++) {
      int id = loc[i][j];
      ss.insert(id);
      auto itr = ss.find(id);
      itr--;
      int le = *itr;
      itr++;
      itr++;
      int ri = *itr;
      ss.erase(id);
      le = le + 1;
      ri = ri - i;
      if (mp[le]) continue;
      mp[le] = true;
      ans += st.query(le, ri + 1);
    }
  }
  cout << ans << endl;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  solve();
  return 0;
}

```