#include <bits/stdc++.h>
using namespace std;
int n;
const int maxn = 1e6 + 10;
int tmp[maxn], num[maxn], l[maxn], r[maxn], c[maxn];
int main() {
  cin >> n;
  int mx = 0, mx_id = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &tmp[i]);
    if (tmp[i] > mx) {
      mx = tmp[i];
      mx_id = i;
    }
  }
  num[0] = num[n] = mx;
  for (int i = 0; i <= n - 1; i++) {
    int k = i + mx_id;
    if (k > n) k -= n;
    num[i] = tmp[k];
  }
  for (int i = 1; i <= n; i++) {
    l[i] = i - 1;
    while (l[i] > 0 && num[i] >= num[l[i]]) l[i] = l[l[i]];
  }
  for (int i = n - 1; i >= 0; i--) {
    r[i] = i + 1;
    while (r[i] < n && num[i] > num[r[i]]) r[i] = r[r[i]];
    if (r[i] < n && num[i] == num[r[i]]) {
      c[i] = c[r[i]] + 1;
      r[i] = r[r[i]];
    }
  }
  long long ans = 0;
  for (int i = 1; i <= n - 1; i++) {
    ans += c[i] + 2;
    if (l[i] == 0 && r[i] == n) ans -= 1;
  }
  cout << ans;
}
