#include <bits/stdc++.h>
using namespace std;
int N;
int P[300010], Q[300010];
struct node {
  int mn1, mn2;
  long long cnt1, cnt2;
};
node mrg(node a, node b) {
  if (a.mn1 == 0) return b;
  if (b.mn1 == 0) return a;
  node ret;
  if (a.mn1 == b.mn1) {
    ret.mn1 = a.mn1;
    ret.cnt1 = a.cnt1 + b.cnt1;
    ret.mn2 = min(a.mn2, b.mn2);
    if (a.mn2 == b.mn2)
      ret.cnt2 = a.cnt2 + b.cnt2;
    else if (a.mn2 < b.mn2)
      ret.cnt2 = a.cnt2;
    else
      ret.cnt2 = b.cnt2;
    return ret;
  }
  if (a.mn1 > b.mn1) swap(a, b);
  ret.mn1 = a.mn1;
  ret.cnt1 = a.cnt1;
  ret.mn2 = min(a.mn2, b.mn1);
  if (a.mn2 == b.mn1)
    ret.cnt2 = a.cnt2 + b.cnt1;
  else if (a.mn2 < b.mn1)
    ret.cnt2 = a.cnt2;
  else
    ret.cnt2 = b.cnt1;
  return ret;
}
struct BIT {
  vector<node> tree;
  vector<int> lazy;
  void init() {
    tree = vector<node>(4 * N, {0, (int)1e9, 1, 0});
    lazy = vector<int>(4 * N, 0);
  }
  void prop(int l, int r, int n) {
    if (l != r) {
      tree[2 * n].mn1 += lazy[n];
      tree[2 * n].mn2 += lazy[n];
      lazy[2 * n] += lazy[n];
      tree[2 * n + 1].mn1 += lazy[n];
      tree[2 * n + 1].mn2 += lazy[n];
      lazy[2 * n + 1] += lazy[n];
      lazy[n] = 0;
    }
  }
  void upd(int a, int b, int d, int l, int r, int n) {
    if (b < l || r < a) return;
    if (a <= l && r <= b) {
      tree[n].mn1 += d;
      tree[n].mn2 += d;
      lazy[n] += d;
      return;
    }
    prop(l, r, n);
    int m = (l + r) >> 1;
    upd(a, b, d, l, m, 2 * n);
    upd(a, b, d, m + 1, r, 2 * n + 1);
    tree[n] = mrg(tree[2 * n], tree[2 * n + 1]);
  }
  node quer(int a, int b, int l, int r, int n) {
    if (b < l || r < a) return {0, 0, 0, 0};
    if (a <= l && r <= b) return tree[n];
    prop(l, r, n);
    int m = (l + r) >> 1;
    node L = quer(a, b, l, m, 2 * n);
    node R = quer(a, b, m + 1, r, 2 * n + 1);
    return mrg(L, R);
  }
} bit;
int main() {
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    scanf("%d", &P[i]);
    Q[--P[i]] = i;
  }
  bit.init();
  long long ans = 0;
  for (int i = N - 1; i >= 0; i--) {
    vector<int> tmp;
    if (Q[i] && P[Q[i] - 1] >= i + 1) tmp.push_back(P[Q[i] - 1]);
    if (Q[i] < N - 1 && P[Q[i] + 1] >= i + 1) tmp.push_back(P[Q[i] + 1]);
    sort(tmp.begin(), tmp.end());
    tmp.push_back(N);
    int p = i + 1;
    for (int j = 0; j < tmp.size(); j++) {
      bit.upd(p, tmp[j] - 1, 1 - j, 0, N - 1, 1);
      p = tmp[j];
    }
    bit.upd(i, i, 1, 0, N - 1, 1);
    node x = bit.quer(i, N - 1, 0, N - 1, 1);
    if (x.mn1 == 1 || x.mn1 == 2) ans += x.cnt1;
    if (x.mn2 == 1 || x.mn2 == 2) ans += x.cnt2;
  }
  printf("%lld", ans - N);
}
