#include <bits/stdc++.h>
using namespace std;
struct Tree {
  int min1, total1, min2, total2, down;
} tree[3333333];
int number[333333], data[333333], pos[333333];
int Rand() { return (rand() << 15) | rand(); }
void Change(int root, int mini, int total) {
  if (mini < tree[root].min1) {
    tree[root].min2 = tree[root].min1;
    tree[root].total2 = tree[root].total1;
    tree[root].min1 = mini;
    tree[root].total1 = total;
  } else if (mini == tree[root].min1) {
    tree[root].total1 += total;
  } else if (mini < tree[root].min2) {
    tree[root].min2 = mini;
    tree[root].total2 = total;
  } else if (mini == tree[root].min2) {
    tree[root].total2 += total;
  }
  return;
}
void Down(int root) {
  if (tree[root].down != 0) {
    tree[root].min1 += tree[root].down;
    tree[root].min2 += tree[root].down;
    tree[root << 1].down += tree[root].down;
    tree[(root << 1) | 1].down += tree[root].down;
    tree[root].down = 0;
  }
  return;
}
void Update(int root) {
  tree[root].min1 = tree[root].min2 = 1111111111;
  tree[root].total1 = tree[root].total2 = 0;
  Change(root, tree[root << 1].min1, tree[root << 1].total1);
  Change(root, tree[root << 1].min2, tree[root << 1].total2);
  Change(root, tree[(root << 1) | 1].min1, tree[(root << 1) | 1].total1);
  Change(root, tree[(root << 1) | 1].min2, tree[(root << 1) | 1].total2);
  return;
}
void Build_Tree(int root, int nowleft, int nowright) {
  int mid = (nowleft + nowright) >> 1;
  if (nowleft == nowright) {
    tree[root].down = 0;
    tree[root].min1 = number[mid];
    tree[root].total1 = 1;
    tree[root].min2 = 1111111111;
    tree[root].total2 = 0;
    return;
  }
  Build_Tree(root << 1, nowleft, mid);
  Build_Tree((root << 1) | 1, mid + 1, nowright);
  Update(root);
  return;
}
void Add(int root, int nowleft, int nowright, int askleft, int askright,
         int delta) {
  int mid = (nowleft + nowright) >> 1;
  Down(root);
  if (nowright < askleft || askright < nowleft) {
    return;
  }
  if (askleft <= nowleft && nowright <= askright) {
    tree[root].down += delta;
    Down(root);
    return;
  }
  Add(root << 1, nowleft, mid, askleft, askright, delta);
  Add((root << 1) | 1, mid + 1, nowright, askleft, askright, delta);
  Update(root);
  return;
}
void Same(int root, int nowleft, int nowright, int askleft, int askright,
          int news) {
  int mid = (nowleft + nowright) >> 1;
  Down(root);
  if (nowright < askleft || askright < nowleft) {
    return;
  }
  if (askleft <= nowleft && nowright <= askright) {
    tree[root].min1 = news;
    tree[root].total1 = 1;
    tree[root].min2 = 1111111111;
    tree[root].total2 = 0;
    return;
  }
  Same(root << 1, nowleft, mid, askleft, askright, news);
  Same((root << 1) | 1, mid + 1, nowright, askleft, askright, news);
  Update(root);
  return;
}
int main() {
  int n, i, x, y;
  long long answer;
  srand((unsigned)time(0));
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d", &data[i]);
    pos[data[i]] = i;
  }
  memset(number, 127, sizeof(number));
  number[1] = 1;
  Build_Tree(1, 1, n);
  answer = 1;
  for (i = 2; i <= n; i++) {
    if (pos[i] == 1) {
      x = data[2];
      if (x >= i) {
        Add(1, 1, n, 1, i - 1, 1);
      } else if (x + 1 <= i - 1) {
        Add(1, 1, n, x + 1, i - 1, 1);
      }
    } else if (pos[i] == n) {
      x = data[n - 1];
      if (x >= i) {
        Add(1, 1, n, 1, i - 1, 1);
      } else if (x + 1 <= i - 1) {
        Add(1, 1, n, x + 1, i - 1, 1);
      }
    } else {
      x = data[pos[i] - 1];
      y = data[pos[i] + 1];
      if (x > y) {
        swap(x, y);
      }
      if (x >= i) {
        Add(1, 1, n, 1, i - 1, 1);
      } else if (y >= i) {
        if (x + 1 <= i - 1) {
          Add(1, 1, n, x + 1, i - 1, 1);
        }
      } else {
        Add(1, 1, n, 1, x, -1);
        if (y + 1 <= i - 1) {
          Add(1, 1, n, y + 1, i - 1, 1);
        }
      }
    }
    Same(1, 1, n, i, i, 1);
    if (tree[1].min1 == 1 || tree[1].min1 == 2) {
      answer += tree[1].total1;
    }
    if (tree[1].min2 == 1 || tree[1].min2 == 2) {
      answer += tree[1].total2;
    }
  }
  printf("%I64d\n", answer - n);
  return 0;
}
