#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
inline void smin(T &a, U b) {
  if (a > b) a = b;
}
template <typename T, typename U>
inline void smax(T &a, U b) {
  if (a < b) a = b;
}
template <class T>
inline void gn(T &first) {
  char c, sg = 0;
  while (c = getchar(), (c > '9' || c < '0') && c != '-')
    ;
  for ((c == '-' ? sg = 1, c = getchar() : 0), first = 0; c >= '0' && c <= '9';
       c = getchar())
    first = (first << 1) + (first << 3) + c - '0';
  if (sg) first = -first;
}
template <class T>
inline void print(T first) {
  if (first < 0) {
    putchar('-');
    return print(-first);
  }
  if (first < 10) {
    putchar('0' + first);
    return;
  }
  print(first / 10);
  putchar(first % 10 + '0');
}
template <class T>
inline void println(T first) {
  print(first);
  putchar('\n');
}
int power(int a, int b, int m, int ans = 1) {
  for (; b; b >>= 1, a = 1LL * a * a % m)
    if (b & 1) ans = 1LL * ans * a % m;
  return ans;
}
int p[300011];
int n;
int pos[300011];
struct mi {
  int val, cnt;
  mi(int val = 0, int cnt = 0) : val(val), cnt(cnt) {}
  bool operator==(mi &u) { return u.val == val; }
  mi operator+(mi &u) {
    mi ans = *this;
    ans.cnt += u.cnt;
    return ans;
  }
  bool operator<(mi &u) { return val < u.val; }
};
mi INF = mi(0x3f3f3f3f, 0);
struct node {
  mi val[2];
  node() {
    val[0] = INF;
    val[1] = INF;
  }
  node operator+(node &u) {
    node ans = *this;
    for (int i = 0; i < 2; i++) {
      if (u.val[i] == ans.val[1]) ans.val[1] = ans.val[1] + u.val[i];
      if (u.val[i] < ans.val[1]) {
        if (u.val[i] < ans.val[0]) {
          swap(ans.val[0], ans.val[1]);
          ans.val[0] = u.val[i];
        } else if (u.val[i] == ans.val[0]) {
          ans.val[0] = ans.val[0] + u.val[i];
        } else
          ans.val[1] = u.val[i];
      }
    }
    return ans;
  }
} mem[300011 << 2];
int flag[300011 << 2];
void push_up(int u) { mem[u] = mem[(u << 1)] + mem[((u << 1) | 1)]; }
inline void add(node &u, int d) {
  u.val[0].val += d;
  u.val[1].val += d;
}
void push_down(int u) {
  if (flag[u] == 0) return;
  flag[(u << 1)] += flag[u];
  flag[((u << 1) | 1)] += flag[u];
  add(mem[(u << 1)], flag[u]);
  add(mem[((u << 1) | 1)], flag[u]);
  flag[u] = 0;
}
void update(int u, int st, int ed, int l, int r, int d) {
  smax(l, st);
  smin(r, ed);
  if (l >= r) return;
  if (l == st and r == ed) {
    add(mem[u], d);
    flag[u] += d;
    return;
  }
  push_down(u);
  update((u << 1), st, (st + ed >> 1), l, r, d);
  update(((u << 1) | 1), (st + ed >> 1), ed, l, r, d);
  push_up(u);
}
void modify(int u, int st, int ed, int l, int r) {
  smax(l, st);
  smin(r, ed);
  if (l >= r) return;
  if (l == st and r == ed) {
    mem[u].val[0] = mi(0, 1);
    return;
  }
  push_down(u);
  modify((u << 1), st, (st + ed >> 1), l, r);
  modify(((u << 1) | 1), (st + ed >> 1), ed, l, r);
  push_up(u);
}
void solve() {
  cin >> n;
  for (int i = 1; i <= n; i++) gn(p[i]), pos[p[i]] = i;
  p[0] = p[n + 1] = 0x3f3f3f3f;
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    int pi = pos[i];
    int mx = max(p[pi - 1], p[pi + 1]), mn = min(p[pi - 1], p[pi + 1]);
    modify(1, 1, n + 1, i, i + 1);
    if (i > mx) {
      update(1, 1, n + 1, mx + 1, i + 1, 1);
      update(1, 1, n + 1, 1, mn + 1, -1);
    } else if (i < mn) {
      update(1, 1, n + 1, 1, i + 1, 1);
    } else {
      update(1, 1, n + 1, mn + 1, i + 1, 1);
    }
    for (int j = 0; j < 2; j++)
      if (mem[1].val[j].val <= 2 and mem[1].val[j].val >= 1) {
        ans += mem[1].val[j].cnt;
      }
  }
  cout << ans - n << endl;
}
int main() {
  solve();
  return 0;
}
