#include <bits/stdc++.h>
using namespace std;
const int nmax = 300005;
struct node {
  int mn1, mn2, cnt1, cnt2, upd;
} arb[4 * nmax], ret;
long long ans;
int p[nmax], wh[nmax];
int M1, M2, aux;
int n, i, j, R, poz;
void mrg(node &A, node B, node C) {
  aux = A.upd;
  A = B;
  A.upd = aux;
  A.mn1 += B.upd;
  A.mn2 += B.upd;
  M1 = C.mn1 + C.upd;
  M2 = C.mn2 + C.upd;
  if (M1 <= A.mn1) {
    if (M1 == A.mn1)
      A.cnt1 += C.cnt1;
    else {
      A.mn2 = A.mn1, A.cnt2 = A.cnt1;
      A.mn1 = M1, A.cnt1 = C.cnt1;
    }
  } else {
    if (M1 <= A.mn2) {
      if (M1 == A.mn2)
        A.cnt2 += C.cnt1;
      else
        A.mn2 = M1, A.cnt2 = C.cnt1;
    }
  }
  if (M2 <= A.mn2) {
    if (M2 == A.mn2)
      A.cnt2 += C.cnt2;
    else
      A.mn2 = M2, A.cnt2 = C.cnt2;
  }
}
void update(int nod, int l, int r, int st, int dr, int val) {
  if (st <= l && r <= dr) {
    arb[nod].upd += val;
    return;
  }
  int m = (l + r) / 2;
  if (arb[nod].upd) {
    update(2 * nod, l, m, l, m, arb[nod].upd);
    update(2 * nod + 1, m + 1, r, m + 1, r, arb[nod].upd);
    arb[nod].upd = 0;
  }
  if (st <= m) update(2 * nod, l, m, st, dr, val);
  if (m < dr) update(2 * nod + 1, m + 1, r, st, dr, val);
  mrg(arb[nod], arb[2 * nod], arb[2 * nod + 1]);
}
void query(int nod, int l, int r, int st, int dr) {
  if (st <= l && r <= dr) {
    mrg(ret, ret, arb[nod]);
    return;
  }
  int m = (l + r) / 2;
  if (arb[nod].upd) {
    update(2 * nod, l, m, l, m, arb[nod].upd);
    update(2 * nod + 1, m + 1, r, m + 1, r, arb[nod].upd);
    arb[nod].upd = 0;
  }
  if (st <= m) query(2 * nod, l, m, st, dr);
  if (m < dr) query(2 * nod + 1, m + 1, r, st, dr);
  mrg(arb[nod], arb[2 * nod], arb[2 * nod + 1]);
}
void build(int nod, int l, int r) {
  if (l == r) {
    arb[nod].cnt1 = 1;
    arb[nod].mn1 = 0;
    arb[nod].mn2 = (1 << 30);
    return;
  }
  int m = (l + r) / 2;
  build(2 * nod, l, m);
  build(2 * nod + 1, m + 1, r);
  mrg(arb[nod], arb[2 * nod], arb[2 * nod + 1]);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> p[i];
    wh[p[i]] = i;
  }
  build(1, 1, n);
  p[n + 1] = n + 1;
  ans = -n;
  for (R = 1; R <= n; R++) {
    poz = wh[R];
    if (poz > 1 && p[poz - 1] < p[poz]) {
      update(1, 1, n, 1, p[poz - 1], -1);
    }
    if (p[poz] < p[poz + 1]) {
      update(1, 1, n, 1, p[poz], 1);
    }
    if (p[poz + 1] < p[poz]) {
      update(1, 1, n, p[poz + 1] + 1, p[poz], 1);
    }
    ret.mn1 = ret.mn2 = (1 << 30);
    query(1, 1, n, 1, R);
    if (ret.mn1 <= 2) ans += 1LL * ret.cnt1;
    if (ret.mn2 <= 2) ans += 1LL * ret.cnt2;
  }
  cout << ans;
  return 0;
}
