#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int n;
int a[N];
int m1, m2, m3;
int x1, x2;
int l1[N], r1[N];
int l2[N], r2[N];
int l3[N], r3[N];
long long solve(int m, int l[], int r[]) {
  long long res = 0;
  for (int i = 2; i < n; i++)
    if (a[i] == m) res += (long long)l[i - 1] * r[i + 1];
  return res;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  m1 = m2 = m3 = 1e9;
  for (int i = 1; i <= n; i++)
    if (m1 > a[i]) m1 = a[i], x1 = i;
  for (int i = 1; i <= n; i++)
    if (i != x1 && m2 > a[i]) m2 = a[i], x2 = i;
  for (int i = 1; i <= n; i++)
    if (i != x1 && i != x2 && m3 > a[i]) m3 = a[i];
  for (int i = 1; i <= n; i++) {
    l1[i] = l1[i - 1] + (a[i] == m1);
    l2[i] = l2[i - 1] + (a[i] == m2);
    l3[i] = l3[i - 1] + (a[i] == m3);
  }
  for (int i = n; i >= 1; i--) {
    r1[i] = r1[i + 1] + (a[i] == m1);
    r2[i] = r2[i + 1] + (a[i] == m2);
    r3[i] = r3[i + 1] + (a[i] == m3);
  }
  long long ans = 0;
  if (m1 == m2 && m2 == m3)
    ans += solve(m2, l1, r3);
  else if (m1 == m2) {
    ans += solve(m2, l1, r3);
    ans += solve(m1, l3, r2);
    ans += solve(m3, l1, r2);
  } else if (m2 == m3) {
    ans += solve(m2, l1, r3);
    ans += solve(m1, l2, r3);
    ans += solve(m3, l2, r1);
  } else {
    ans += solve(m2, l1, r3);
    ans += solve(m3, l1, r2);
    ans += solve(m1, l2, r3);
    ans += solve(m3, l2, r1);
    ans += solve(m1, l3, r2);
    ans += solve(m2, l3, r1);
  }
  printf("%I64d\n", ans);
}
