#include <bits/stdc++.h>
using namespace std;
int n;
int val[360008];
long long ans = 0;
int cntl[360008], cntr[360008], maxl[360008], minl[360008], cnt[360008 * 2];
int exist(int x, int l, int r) { return l <= x and x <= r; }
void solve(int l, int r) {
  if (l == r) {
    if (val[l] == 1) ans++;
    return;
  }
  int mid = (l + r) / 2;
  maxl[mid] = val[mid];
  for (int i = mid - 1; i >= l; i--) maxl[i] = max(maxl[i + 1], val[i]);
  maxl[mid + 1] = val[mid + 1];
  for (int i = mid + 2; i <= r; i++) maxl[i] = max(maxl[i - 1], val[i]);
  int rpt = mid;
  for (int i = mid + 1; i <= r; i++)
    if (cntr[val[i]] == 0) {
      cntr[val[i]]++, cnt[maxl[i] - (i - mid) + 350005]++;
      rpt = i;
    } else
      break;
  int spj = mid + 1;
  for (int i = mid; i >= l; i--) {
    if (cntl[val[i]]) break;
    cntl[val[i]]++;
    while (rpt > mid and cntr[val[i]])
      cntr[val[rpt]]--, cnt[maxl[rpt] - (rpt - mid) + 350005] -= (rpt >= spj),
          rpt--;
    if (rpt <= mid) break;
    if (spj > rpt) spj = rpt + 1;
    while (spj <= rpt and maxl[spj] <= maxl[i])
      cnt[maxl[spj] - (spj - mid) + 350005]--, spj++;
    ans += cnt[mid - i + 1 + 350005];
    ans += exist(maxl[i] - (mid - i + 1), 1, spj - mid - 1);
  }
  for (int i = l; i <= r; i++)
    cntl[val[i]] = cntr[val[i]] = cnt[maxl[i] - (i - mid) + 350005] = 0;
  solve(l, mid);
  solve(mid + 1, r);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &val[i]);
  solve(1, n);
  printf("%lld\n", ans);
  return 0;
}
