#include <bits/stdc++.h>
using namespace std;
int n, root = 0, tot = 0;
int a[1000010], pos[1000010];
long long ans = 0;
struct Tree {
  int l, r, min1, min2, sum1, sum2, tag;
} t[1000010];
inline int read() {
  int ans = 0, k = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') k = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    ans = (ans << 3) + (ans << 1) + (c - '0');
    c = getchar();
  }
  return ans * k;
}
inline void update(int now) {
  t[now].min1 = min(t[t[now].l].min1, t[t[now].r].min1);
  if (t[t[now].l].min1 == t[t[now].r].min1) {
    t[now].sum1 = t[t[now].l].sum1 + t[t[now].r].sum1;
    t[now].min2 = min(t[t[now].l].min2, t[t[now].r].min2);
    t[now].sum2 = t[t[now].l].sum2 * (t[t[now].l].min2 <= t[t[now].r].min2) +
                  t[t[now].r].sum2 * (t[t[now].r].min2 <= t[t[now].l].min2);
  } else {
    if (t[t[now].l].min1 < t[t[now].r].min1) {
      t[now].sum1 = t[t[now].l].sum1;
      t[now].min2 = min(t[t[now].l].min2, t[t[now].r].min1);
      t[now].sum2 = t[t[now].l].sum2 * (t[t[now].l].min2 <= t[t[now].r].min1) +
                    t[t[now].r].sum1 * (t[t[now].r].min1 <= t[t[now].l].min2);
    } else {
      t[now].sum1 = t[t[now].r].sum1;
      t[now].min2 = min(t[t[now].r].min2, t[t[now].l].min1);
      t[now].sum2 = t[t[now].r].sum2 * (t[t[now].r].min2 <= t[t[now].l].min1) +
                    t[t[now].l].sum1 * (t[t[now].l].min1 <= t[t[now].r].min2);
    }
  }
}
inline void pushdown(int now) {
  if (t[now].tag) {
    t[t[now].l].tag += t[now].tag;
    t[t[now].r].tag += t[now].tag;
    t[t[now].l].min1 += t[now].tag;
    t[t[now].r].min1 += t[now].tag;
    t[t[now].l].min2 += t[now].tag;
    t[t[now].r].min2 += t[now].tag;
    t[now].tag = 0;
  }
}
void build(int &now, int l, int r) {
  now = ++tot;
  if (l == r) {
    t[now].min1 = 0;
    t[now].sum1 = 1;
    t[now].min2 = 1;
    t[now].sum2 = 0;
    return;
  }
  int mid = (l + r) >> 1;
  build(t[now].l, l, mid);
  build(t[now].r, mid + 1, r);
  update(now);
}
void add(int now, int l, int r, int p, int q, int x) {
  if (p > q) return;
  if (p <= l && q >= r) {
    t[now].min1 += x;
    t[now].min2 += x;
    t[now].tag += x;
    return;
  }
  pushdown(now);
  int mid = (l + r) >> 1;
  if (p <= mid) add(t[now].l, l, mid, p, q, x);
  if (q > mid) add(t[now].r, mid + 1, r, p, q, x);
  update(now);
}
int query(int now, int l, int r, int p, int q) {
  if (p > q) return 0;
  if (p <= l && q >= r)
    return t[now].sum1 * (t[now].min1 == 1 || t[now].min1 == 2) +
           t[now].sum2 * (t[now].min2 == 2);
  pushdown(now);
  int mid = (l + r) >> 1;
  int res = 0;
  if (p <= mid) res += query(t[now].l, l, mid, p, q);
  if (q > mid) res += query(t[now].r, mid + 1, r, p, q);
  return res;
}
int main() {
  n = read();
  build(root, 1, n);
  for (register int i = 1; i <= n; i++) a[read()] = i;
  for (register int i = 1; i <= n; i++) pos[a[i]] = i;
  pos[0] = pos[n + 1] = 1e9;
  for (register int i = 1; i <= n; i++) {
    add(root, 1, n, i, i, 1);
    int l = pos[a[i] - 1], r = pos[a[i] + 1];
    if (l > r) swap(l, r);
    if (l > i) {
      add(root, 1, n, 1, i - 1, 1);
    } else if (r > i) {
      add(root, 1, n, l + 1, i - 1, 1);
    } else {
      add(root, 1, n, r + 1, i - 1, 1);
      add(root, 1, n, 1, l, -1);
    }
    ans += 1ll * query(root, 1, n, 1, i - 1);
  }
  printf("%lld", ans);
  return 0;
}

### Prompt

Develop a solution in CPP to the problem described below:

Nick has some permutation consisting of p integers from 1 to n. A segment [l, r] (l ≤ r) is a set of elements pi satisfying l ≤ i ≤ r.

Nick calls a pair of segments [a0, a1] and [b0, b1] (1 ≤ a0 ≤ a1 < b0 ≤ b1 ≤ n) good if all their (a1 - a0 + b1 - b0 + 2) elements, when sorted in ascending order, form an arithmetic progression with a difference of 1. That is, when they sorted in ascending order, the elements are in the form {x, x + 1, x + 2, ..., x + m - 1}, for some x and m.

Your task is to find the number of distinct pairs of good segments in the given permutation. Two pairs of segments are considered distinct if the sets of elements contained in these pairs of segments are distinct. For example, any segment [l, r] (l < r) can be represented as a pair of segments, as [l, i] and [i + 1, r] (l ≤ i ≤ r). As all these pairs consist of the same set of elements, they are considered identical.

See the notes accompanying the sample tests for clarification.

Input

The first line contains integer n (1 ≤ n ≤ 3·105) — the permutation size. The second line contains n space-separated distinct integers pi, (1 ≤ pi ≤ n).

Output

Print a single integer — the number of good pairs of segments of permutation p.

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. 

Examples

Input

3
1 2 3


Output

3


Input

5
1 4 5 3 2


Output

10


Input

5
5 4 3 1 2


Output

10

Note

In the first sample the following pairs of segments are good: ([1, 1], [2, 2]); ([2, 2], [3, 3]); ([1, 2], [3, 3]). Pair of segments ([1, 1], [2, 3]) is by definition equivalent to pair ([1, 2], [3, 3]), since both of them covers the same set of elements, namely {1, 2, 3}.

In the third sample the following pairs of segments are good: ([4, 4], [5, 5]); ([3, 3],[4, 5]); ([2, 2],[3, 5]); ([1, 1],[2, 5]); ([3, 3],[5, 5]); ([2, 3],[5, 5]); ([1, 3],[5, 5]); ([2, 2],[3, 3]); ([1, 1],[2, 3]); ([1, 1],[2, 2]). 

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
int n, root = 0, tot = 0;
int a[1000010], pos[1000010];
long long ans = 0;
struct Tree {
  int l, r, min1, min2, sum1, sum2, tag;
} t[1000010];
inline int read() {
  int ans = 0, k = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') k = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    ans = (ans << 3) + (ans << 1) + (c - '0');
    c = getchar();
  }
  return ans * k;
}
inline void update(int now) {
  t[now].min1 = min(t[t[now].l].min1, t[t[now].r].min1);
  if (t[t[now].l].min1 == t[t[now].r].min1) {
    t[now].sum1 = t[t[now].l].sum1 + t[t[now].r].sum1;
    t[now].min2 = min(t[t[now].l].min2, t[t[now].r].min2);
    t[now].sum2 = t[t[now].l].sum2 * (t[t[now].l].min2 <= t[t[now].r].min2) +
                  t[t[now].r].sum2 * (t[t[now].r].min2 <= t[t[now].l].min2);
  } else {
    if (t[t[now].l].min1 < t[t[now].r].min1) {
      t[now].sum1 = t[t[now].l].sum1;
      t[now].min2 = min(t[t[now].l].min2, t[t[now].r].min1);
      t[now].sum2 = t[t[now].l].sum2 * (t[t[now].l].min2 <= t[t[now].r].min1) +
                    t[t[now].r].sum1 * (t[t[now].r].min1 <= t[t[now].l].min2);
    } else {
      t[now].sum1 = t[t[now].r].sum1;
      t[now].min2 = min(t[t[now].r].min2, t[t[now].l].min1);
      t[now].sum2 = t[t[now].r].sum2 * (t[t[now].r].min2 <= t[t[now].l].min1) +
                    t[t[now].l].sum1 * (t[t[now].l].min1 <= t[t[now].r].min2);
    }
  }
}
inline void pushdown(int now) {
  if (t[now].tag) {
    t[t[now].l].tag += t[now].tag;
    t[t[now].r].tag += t[now].tag;
    t[t[now].l].min1 += t[now].tag;
    t[t[now].r].min1 += t[now].tag;
    t[t[now].l].min2 += t[now].tag;
    t[t[now].r].min2 += t[now].tag;
    t[now].tag = 0;
  }
}
void build(int &now, int l, int r) {
  now = ++tot;
  if (l == r) {
    t[now].min1 = 0;
    t[now].sum1 = 1;
    t[now].min2 = 1;
    t[now].sum2 = 0;
    return;
  }
  int mid = (l + r) >> 1;
  build(t[now].l, l, mid);
  build(t[now].r, mid + 1, r);
  update(now);
}
void add(int now, int l, int r, int p, int q, int x) {
  if (p > q) return;
  if (p <= l && q >= r) {
    t[now].min1 += x;
    t[now].min2 += x;
    t[now].tag += x;
    return;
  }
  pushdown(now);
  int mid = (l + r) >> 1;
  if (p <= mid) add(t[now].l, l, mid, p, q, x);
  if (q > mid) add(t[now].r, mid + 1, r, p, q, x);
  update(now);
}
int query(int now, int l, int r, int p, int q) {
  if (p > q) return 0;
  if (p <= l && q >= r)
    return t[now].sum1 * (t[now].min1 == 1 || t[now].min1 == 2) +
           t[now].sum2 * (t[now].min2 == 2);
  pushdown(now);
  int mid = (l + r) >> 1;
  int res = 0;
  if (p <= mid) res += query(t[now].l, l, mid, p, q);
  if (q > mid) res += query(t[now].r, mid + 1, r, p, q);
  return res;
}
int main() {
  n = read();
  build(root, 1, n);
  for (register int i = 1; i <= n; i++) a[read()] = i;
  for (register int i = 1; i <= n; i++) pos[a[i]] = i;
  pos[0] = pos[n + 1] = 1e9;
  for (register int i = 1; i <= n; i++) {
    add(root, 1, n, i, i, 1);
    int l = pos[a[i] - 1], r = pos[a[i] + 1];
    if (l > r) swap(l, r);
    if (l > i) {
      add(root, 1, n, 1, i - 1, 1);
    } else if (r > i) {
      add(root, 1, n, l + 1, i - 1, 1);
    } else {
      add(root, 1, n, r + 1, i - 1, 1);
      add(root, 1, n, 1, l, -1);
    }
    ans += 1ll * query(root, 1, n, 1, i - 1);
  }
  printf("%lld", ans);
  return 0;
}

```