#include <bits/stdc++.h>
using namespace std;
const int OFFSET = 5;
const int MAXN = 1000000;
int N;
int heights[MAXN], r[MAXN], l[MAXN], c[MAXN];
int main() {
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    scanf("%d", &heights[i]);
  }
  rotate(heights, max_element(heights, heights + N), heights + N);
  heights[N] = heights[0];
  for (int i = N - 1; i >= 0; i--) {
    r[i] = i + 1;
    while (r[i] < N && heights[i] > heights[r[i]]) {
      r[i] = r[r[i]];
    }
    if (r[i] < N && heights[i] == heights[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 && heights[i] >= heights[l[i]]) {
      l[i] = l[l[i]];
    }
  }
  long long ans = 0;
  for (int i = 0; i < N; i++) {
    ans += c[i];
    if (heights[i] < heights[0]) {
      if (l[i] == 0 && r[i] == N) {
        ans++;
      } else {
        ans += 2;
      }
    }
  }
  printf("%I64d\n", ans);
  return 0;
}
