#include <bits/stdc++.h>
long long h[1000001], hh[1000001], c[1000001], r[1000001], l[1000001];
int main() {
  long long n, maxx, maxi, ans;
  scanf("%lld", &n);
  for (long long i = 0; i < n; i++) scanf("%lld", &h[i]);
  maxi = 0;
  maxx = h[0];
  for (long long i = 1; i < n; i++)
    if (h[i] > maxx) {
      maxx = h[i];
      maxi = i;
    }
  for (long long i = 0; i < n; i++) hh[i] = h[(maxi + i) % n];
  hh[n] = hh[0];
  c[n] = 0;
  for (long long i = n - 1; i >= 0; i--) {
    r[i] = i + 1;
    while (r[i] < n && hh[i] > hh[r[i]]) r[i] = r[r[i]];
    if (r[i] < n && hh[i] == hh[r[i]]) {
      c[i] = c[r[i]] + 1;
      r[i] = r[r[i]];
    }
  }
  l[0] = 0;
  for (long long i = 1; i <= n; i++) {
    l[i] = i - 1;
    while (l[i] > 0 && hh[i] >= hh[l[i]]) l[i] = l[l[i]];
  }
  ans = 0;
  for (long long i = 0; i < n; i++) {
    ans += c[i];
    if (hh[i] < hh[0]) {
      if (l[i] == 0 && r[i] == n)
        ans++;
      else
        ans += 2;
    }
  }
  printf("%lld\n", ans);
  return 0;
}
