#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5;
long long nu[maxn], num[maxn];
int nul[maxn], nur[maxn], nus[maxn];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, maxi = 0;
  cin >> n;
  long long maxx = 0;
  for (int i = 0; i < n; ++i) {
    cin >> nu[i];
    if (nu[i] > maxx) {
      maxx = nu[i];
      maxi = i;
    }
  }
  --maxi;
  for (int i = 1; i <= n; ++i) num[i] = nu[(maxi + i) % n];
  for (int i = 2; i <= n; ++i) {
    nul[i] = i - 1;
    while (nul[i] > 1 && num[i] >= num[nul[i]]) {
      nul[i] = nul[nul[i]];
    }
  }
  for (int i = n; i >= 1; i--) {
    nur[i] = i + 1;
    while (nur[i] <= n && num[i] > num[nur[i]]) {
      nur[i] = nur[nur[i]];
    }
    if (nur[i] <= n && num[nur[i]] == num[i]) {
      nus[i] = nus[nur[i]] + 1;
      nur[i] = nur[nur[i]];
    }
  }
  long long ans = 0;
  for (int i = 2; i <= n; ++i) {
    ans += nus[i] + 2;
    if (nul[i] == 1 && nur[i] == n + 1) --ans;
  }
  cout << ans << endl;
  return 0;
}
