#include <bits/stdc++.h>
using namespace std;
int n;
int a[300010], b[300010];
struct Info {
  int f, nf, s, ns;
} t[300010 * 4];
Info merge(Info a, Info b) {
  Info c;
  if (a.f < b.f) {
    c.f = a.f, c.nf = a.nf;
    if (a.s < b.f)
      c.s = a.s, c.ns = a.ns;
    else if (a.s > b.f)
      c.s = b.f, c.ns = b.nf;
    else
      c.s = a.s, c.ns = a.ns + b.nf;
  } else if (a.f > b.f) {
    c.f = b.f, c.nf = b.nf;
    if (a.f < b.s)
      c.s = a.f, c.ns = a.nf;
    else if (a.f > b.s)
      c.s = b.s, c.ns = b.ns;
    else
      c.s = a.f, c.ns = a.nf + b.ns;
  } else {
    c.f = a.f, c.nf = a.nf + b.nf;
    if (a.s < b.s)
      c.s = a.s, c.ns = a.ns;
    else if (a.s > b.s)
      c.s = b.s, c.ns = b.ns;
    else
      c.s = a.s, c.ns = a.ns + b.ns;
  }
  return c;
}
int tag[300010 * 4];
Info res;
void pd(int k) {
  if (tag[k]) {
    t[k << 1].f += tag[k], t[k << 1].s += tag[k];
    t[k << 1 | 1].f += tag[k], t[k << 1 | 1].s += tag[k];
    tag[k << 1] += tag[k], tag[k << 1 | 1] += tag[k];
    tag[k] = 0;
  }
}
void build(int k, int l, int r) {
  if (l == r) {
    t[k] = {0, 1, 1000000000, 0};
    return;
  }
  int mid = l + r >> 1;
  build(k << 1, l, mid), build(k << 1 | 1, mid + 1, r);
  t[k] = merge(t[k << 1], t[k << 1 | 1]);
}
void add(int k, int l, int r, int st, int en, int c) {
  if (st <= l && r <= en) {
    t[k].f += c, t[k].s += c;
    tag[k] += c;
    return;
  }
  pd(k);
  int mid = l + r >> 1;
  if (st <= mid) add(k << 1, l, mid, st, en, c);
  if (mid < en) add(k << 1 | 1, mid + 1, r, st, en, c);
  t[k] = merge(t[k << 1], t[k << 1 | 1]);
}
void query(int k, int l, int r, int st, int en) {
  if (st <= l && r <= en) {
    res = merge(res, t[k]);
    return;
  }
  pd(k);
  int mid = l + r >> 1;
  if (st <= mid) query(k << 1, l, mid, st, en);
  if (mid < en) query(k << 1 | 1, mid + 1, r, st, en);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), b[a[i]] = i;
  long long ans = 0;
  build(1, 1, n);
  for (int i = 1; i <= n; ++i) {
    int x = b[i];
    add(1, 1, n, 1, i, 1);
    if (x - 1 > 0 && a[x - 1] < i) add(1, 1, n, 1, a[x - 1], -1);
    if (x + 1 <= n && a[x + 1] < i) add(1, 1, n, 1, a[x + 1], -1);
    res = {1000000000, 0, 1000000000 + 1, 0};
    query(1, 1, n, 1, i);
    ans += (res.f <= 2 ? res.nf + (res.s <= 2 ? res.ns : 0) : 0);
  }
  printf("%lld\n", ans - n);
  return 0;
}
