#include <bits/stdc++.h>
using namespace std;
const long long inf = INT_MAX, df = 2e6 + 7;
long long le[df], rig[df], cnt[df], sta[df], a[df], b[df], top, i, j, k, l, m,
    n, o, p, q, r, s, t, u, v, w, x, y, z, ans;
inline long long read() {
  long long x = 0, y = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') y = (ch == '-') ? -1 : 1, ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
  return x * y;
}
int main() {
  n = read();
  for (long long i = (0); i <= (n - 1); ++i) a[i] = read();
  p = 0;
  for (long long i = (0); i <= (n - 1); ++i)
    if (a[i] > a[p]) p = i;
  for (long long i = (0); i <= (n - 1); ++i) b[i] = a[(i + p) % n];
  b[n] = a[p];
  for (long long i = (0); i <= (n); ++i) a[i + 1] = b[i];
  n++;
  for (long long i = (2); i <= (n); ++i) {
    le[i] = i - 1;
    while (le[i] != 1 && a[le[i]] <= a[i]) le[i] = le[le[i]];
  }
  for (long long i = (n - 1); i >= (1); --i) {
    rig[i] = i + 1;
    while (rig[i] != n && a[rig[i]] < a[i]) rig[i] = rig[rig[i]];
    if (rig[i] != n && a[i] == a[rig[i]]) {
      cnt[i] = cnt[rig[i]] + 1;
      rig[i] = rig[rig[i]];
    }
  }
  for (long long i = (1); i <= (n); ++i) {
    ans += cnt[i];
    if (a[i] < a[1]) {
      ans += 2;
      if (le[i] == 1 && rig[i] == n) --ans;
    }
  }
  return printf("%lld\n", ans), 0;
}
