#include <bits/stdc++.h>
const int n = 300005;
int N, i;
int A[n], B[n];
long long Ans;
struct Node {
  int Min1, Min2, Cnt1, Cnt2, V;
} f[4 * n], T;
void Down(Node A, Node &B, Node &C) {
  B.Min1 += A.V, B.Min2 += A.V, B.V += A.V;
  C.Min1 += A.V, C.Min2 += A.V, C.V += A.V;
  A.V = 0;
}
void Updata(Node &A, Node B, Node C) {
  if (B.Min1 < C.Min1)
    A.Min1 = B.Min1, A.Min2 = B.Min2;
  else
    A.Min1 = C.Min1, A.Min2 = C.Min2;
  if (B.Min1 != A.Min1 && B.Min1 < A.Min2) A.Min2 = B.Min1;
  if (C.Min1 != A.Min1 && C.Min1 < A.Min2) A.Min2 = C.Min1;
  if (B.Min2 < A.Min2) A.Min2 = B.Min2;
  if (C.Min2 < A.Min2) A.Min2 = C.Min2;
  A.Cnt1 = 0;
  if (B.Min1 == A.Min1) A.Cnt1 += B.Cnt1;
  if (C.Min1 == A.Min1) A.Cnt1 += C.Cnt1;
  A.Cnt2 = 0;
  if (B.Min1 == A.Min2) A.Cnt2 += B.Cnt1;
  if (C.Min1 == A.Min2) A.Cnt2 += C.Cnt1;
  if (B.Min2 == A.Min2) A.Cnt2 += B.Cnt2;
  if (C.Min2 == A.Min2) A.Cnt2 += C.Cnt2;
  A.V = 0;
}
void Build(int t, int l, int r) {
  int mid = (l + r) / 2;
  if (l == r) {
    f[t].Min1 = 1, f[t].Cnt1 = 1;
    f[t].Min2 = 100000000, f[t].Cnt2 = 0;
    f[t].V = 0;
    return;
  }
  Build(2 * t, l, mid), Build(2 * t + 1, mid + 1, r);
  Updata(f[t], f[2 * t], f[2 * t + 1]);
}
void Modify(int t, int l, int r, int ll, int rr, int x) {
  int mid = (l + r) / 2;
  if (l == ll && r == rr) {
    f[t].Min1 += x, f[t].Min2 += x, f[t].V += x;
    return;
  }
  if (f[t].V) Down(f[t], f[2 * t], f[2 * t + 1]);
  if (rr <= mid)
    Modify(2 * t, l, mid, ll, rr, x);
  else if (ll > mid)
    Modify(2 * t + 1, mid + 1, r, ll, rr, x);
  else {
    Modify(2 * t, l, mid, ll, mid, x);
    Modify(2 * t + 1, mid + 1, r, mid + 1, rr, x);
  }
  Updata(f[t], f[2 * t], f[2 * t + 1]);
}
Node Query(int t, int l, int r, int ll, int rr) {
  int mid = (l + r) / 2;
  Node f1, f2, f3;
  if (l == ll && r == rr) return f[t];
  if (f[t].V) Down(f[t], f[2 * t], f[2 * t + 1]);
  if (rr <= mid) return Query(2 * t, l, mid, ll, rr);
  if (ll > mid) return Query(2 * t + 1, mid + 1, r, ll, rr);
  f1 = Query(2 * t, l, mid, ll, mid);
  f2 = Query(2 * t + 1, mid + 1, r, mid + 1, rr);
  Updata(f3, f1, f2);
  return f3;
}
int main() {
  scanf("%d", &N);
  for (i = 1; i <= N; ++i) {
    scanf("%d", &A[i]);
    B[A[i]] = i;
  }
  Build(1, 1, N);
  for (i = 2; i <= N; ++i) {
    Modify(1, 1, N, 1, i - 1, 1);
    if (B[i] > 1 && A[B[i] - 1] < i) Modify(1, 1, N, 1, A[B[i] - 1], -1);
    if (B[i] < N && A[B[i] + 1] < i) Modify(1, 1, N, 1, A[B[i] + 1], -1);
    T = Query(1, 1, N, 1, i - 1);
    if (T.Min1 <= 2) Ans += T.Cnt1;
    if (T.Min2 <= 2) Ans += T.Cnt2;
  }
  printf("%I64d\n", Ans);
  return 0;
}
