#include <bits/stdc++.h>
using namespace std;
int root, cnt = 0, n, p[300010], b[300010];
long long ans = 0;
struct node {
  int ch[2], mn, tot, tot1, tag;
} a[700010];
void build(int &u, int l, int r) {
  a[u = ++cnt].tot = r - l + 1;
  if (l == r) return;
  int mid = (l + r) / 2;
  build(a[u].ch[0], l, mid);
  build(a[u].ch[1], mid + 1, r);
}
void update(int u) {
  a[u].mn = min(a[a[u].ch[0]].mn, a[a[u].ch[1]].mn);
  a[u].tot = a[a[u].ch[0]].tot * (a[a[u].ch[0]].mn == a[u].mn) +
             a[a[u].ch[1]].tot * (a[a[u].ch[1]].mn == a[u].mn);
  a[u].tot1 = a[a[u].ch[0]].tot * (a[a[u].ch[0]].mn == a[u].mn + 1) +
              a[a[u].ch[1]].tot * (a[a[u].ch[1]].mn == a[u].mn + 1);
  a[u].tot1 += a[a[u].ch[0]].tot1 * (a[a[u].ch[0]].mn == a[u].mn) +
               a[a[u].ch[1]].tot1 * (a[a[u].ch[1]].mn == a[u].mn);
}
void pushdown(int u) {
  if (!a[u].tag) return;
  a[a[u].ch[0]].tag += a[u].tag;
  a[a[u].ch[1]].tag += a[u].tag;
  a[a[u].ch[0]].mn += a[u].tag;
  a[a[u].ch[1]].mn += a[u].tag;
  a[u].tag = 0;
}
void modify(int u, int l, int r, int ql, int qr, int d) {
  if (l == ql && r == qr) return a[u].mn += d, a[u].tag += d, void();
  int mid = (l + r) / 2;
  pushdown(u);
  if (qr <= mid)
    modify(a[u].ch[0], l, mid, ql, qr, d);
  else if (ql > mid)
    modify(a[u].ch[1], mid + 1, r, ql, qr, d);
  else
    modify(a[u].ch[0], l, mid, ql, mid, d),
        modify(a[u].ch[1], mid + 1, r, mid + 1, qr, d);
  update(u);
}
int query(int u, int l, int r, int ql, int qr) {
  int ret = 0, mid = (l + r) / 2;
  if (l == ql && r == qr) {
    if (a[u].mn == 1) ret = a[u].tot + a[u].tot1;
    if (a[u].mn == 2) ret = a[u].tot;
    return ret;
  }
  pushdown(u);
  if (qr <= mid) return query(a[u].ch[0], l, mid, ql, qr);
  if (ql > mid) return query(a[u].ch[1], mid + 1, r, ql, qr);
  return query(a[u].ch[0], l, mid, ql, mid) +
         query(a[u].ch[1], mid + 1, r, mid + 1, qr);
}
int main() {
  int x;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &x), p[x] = i;
  build(root, 1, n);
  for (int i = n, x, y; i >= 1; i--) {
    b[p[i]] = i;
    x = b[p[i] - 1];
    y = b[p[i] + 1];
    if (x > y) swap(x, y);
    if (!y)
      modify(root, 1, n, i, n, 1);
    else if (!x)
      modify(root, 1, n, i, y - 1, 1);
    else
      modify(root, 1, n, i, x - 1, 1), modify(root, 1, n, y, n, -1);
    ans += query(root, 1, n, i, n);
  }
  printf("%I64d", ans - n);
  return 0;
}
