#include <bits/stdc++.h>
using namespace std;
long long h[1000001], n, l[1000001], r[1000001], p, a[1000001], cnt[1000001],
    ans;
int main() {
  scanf("%lld", &n);
  for (int i = 0; i < n; i++) {
    scanf("%lld", &a[i]);
    if (a[p] < a[i]) p = i;
  }
  for (int i = 0; i <= n; i++) h[i] = a[(i + p) % n];
  for (int i = 1; i <= n; i++) {
    l[i] = i - 1;
    while (l[i] > 0 && h[l[i]] <= h[i]) l[i] = l[l[i]];
  }
  for (int i = n - 1; i >= 0; i--) {
    r[i] = i + 1;
    while (r[i] < n && h[r[i]] < h[i]) r[i] = r[r[i]];
    if (r[i] < n && h[i] == h[r[i]]) {
      cnt[i] = cnt[r[i]] + 1;
      r[i] = r[r[i]];
    }
  }
  for (int i = 0; i < n; i++) {
    ans += cnt[i];
    if (h[i] < h[0]) {
      ans += 2;
      if (l[i] == 0 && r[i] == n) ans--;
    }
  }
  printf("%I64d", ans);
}
