#include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9 + 7;
const int MAXN = (int)1e6 + 7;
const long long inf = (long long)1e18;
const int infint = (int)2e9;
long long a[MAXN], b[MAXN], last[MAXN], lft[MAXN];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  long long mx = -1, ind = -1;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] > mx) {
      mx = a[i];
      ind = i;
    }
  }
  long long t = 0;
  for (int i = ind; i < n; i++) {
    b[i - ind] = a[i];
    t++;
  }
  for (int i = 0; i < ind; i++) {
    b[t] = a[i];
    t++;
  }
  long long ans = 0;
  stack<long long> S;
  for (int i = 0; i < n; i++) {
    while (!S.empty() and b[S.top()] <= b[i]) {
      if (b[S.top()] == b[i] and !last[i]) last[i] = last[S.top()] + 1;
      ans += last[S.top()] + 1;
      S.pop();
    }
    if (S.empty())
      lft[i] = -1;
    else
      lft[i] = S.top();
    if (!S.empty()) ans++;
    S.push(i);
  }
  mx = -1;
  for (int i = n - 1; i >= 1; i--) {
    if (lft[i] > 0 and b[i] >= mx) ans++;
    mx = max(mx, b[i]);
  }
  cout << ans;
}
