#include <bits/stdc++.h>
const int N = 1000005;
int h[N], a[N], l[N], r[N], c[N];
int main() {
  int n, idx;
  scanf("%d", &n);
  for (int i = 1, Max = -1; i <= n; ++i) {
    scanf("%d", &h[i]);
    if (h[i] > Max) {
      Max = h[i];
      idx = i;
    }
  }
  a[1] = h[idx];
  int cnt = 1;
  for (int i = idx + 1; i <= n; ++i) a[++cnt] = h[i];
  for (int i = 1; i < idx; ++i) a[++cnt] = h[i];
  long long ans = 0;
  for (int i = n; i > 0; --i) {
    r[i] = i + 1;
    while (r[i] <= n && a[i] > a[r[i]]) r[i] = r[r[i]];
    if (r[i] <= n && a[i] == a[r[i]]) {
      c[i] = c[r[i]] + 1;
      r[i] = r[r[i]];
    }
  }
  for (int i = 1; i <= n; ++i) {
    l[i] = i - 1;
    while (l[i] > 0 && a[i] >= a[l[i]]) l[i] = l[l[i]];
  }
  for (int i = 1; i <= n; ++i) {
    ans += c[i];
    if (a[i] == a[1]) continue;
    if (r[i] == n + 1 && l[i] == 1)
      ans++;
    else
      ans += 2;
  }
  printf("%I64d\n", ans);
  return 0;
}
