#include <bits/stdc++.h>
using namespace std;
inline long long c2(int n) { return n * (n - 1LL) / 2LL; }
int main() {
  if (fopen("input.txt", "r")) {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
  }
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  vector<int> hh(n);
  int imax = 1;
  for (int i = 0; i != n; ++i) {
    cin >> hh[i];
    if (hh[i] > hh[imax]) imax = i;
  }
  vector<int> h(n + 1);
  for (int i = 0; i != n; ++i) h[i] = hh[(imax + i) % n];
  h[n] = hh[imax];
  vector<int> r(n + 1);
  vector<int> l(n + 1);
  vector<int> c(n + 1);
  r[n] = n;
  l[n] = 0;
  r[0] = n;
  l[0] = 0;
  for (int i = n - 1; i >= 0; --i) {
    r[i] = i + 1;
    while (r[i] < n && h[i] > h[r[i]]) r[i] = r[r[i]];
    if (r[i] < n && h[i] == h[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 && h[i] > h[l[i]]) l[i] = l[l[i]];
    if (l[i] > 0 && h[i] == h[l[i]]) l[i] = l[l[i]];
  }
  long long cnt = 0;
  for (int i = 0; i != n; ++i) {
    cnt += c[i];
    if (h[i] < h[0]) {
      if (l[i] == 0 && r[i] == n)
        cnt += 1;
      else
        cnt += 2;
    }
  }
  cout << cnt;
}
