#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1000000;
int n, h[MAX_N], p[MAX_N], l[MAX_N], r[MAX_N], c[MAX_N];
long long ans;
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", &h[i]);
  int tmp = 0;
  for (int i = 1; i < n; i++)
    if (h[i] > h[tmp]) tmp = i;
  for (int i = 0; i < n; i++) {
    p[i] = h[tmp];
    tmp = (tmp + 1) % n;
  }
  for (int i = n - 1; i >= 0; i--) {
    r[i] = i + 1;
    while (r[i] < n && p[r[i]] < p[i]) r[i] = r[r[i]];
    if (r[i] < n && p[r[i]] == p[i]) {
      c[i] = c[r[i]] + 1;
      r[i] = r[r[i]];
    }
  }
  for (int i = 0; i < n; i++) {
    ans += (long long)c[i];
    if (p[i] != p[0]) {
      l[i] = i - 1;
      while (l[i] > 0 && p[l[i]] <= p[i]) l[i] = l[l[i]];
      ans += 2LL;
      if (l[i] == 0 && r[i] == n) ans--;
    }
  }
  printf(
      "%lld"
      "\n",
      ans);
  return 0;
}
