#include <bits/stdc++.h>
using namespace std;
int n, a[1000005], s[1000005], z[1000005], k[1000005];
long long ans;
struct xx {
  int n, v;
} f[1000005];
struct xxx {
  int l, r, z, s;
} p[4000005];
bool cmp(const xx &a, const xx &b) { return a.v > b.v; }
void build(int u, int l, int r) {
  p[u].l = l;
  p[u].r = r;
  p[u].z = (z[l] - z[r + 1]);
  p[u].s = (s[r] - s[l - 1]);
  if (l == r) {
    k[l] = u;
    return;
  }
  int mid = (l + r) >> 1;
  build(u * 2, l, mid);
  build(u * 2 + 1, mid + 1, r);
}
void update(int u) {
  if (u == 1) return;
  int f = u / 2;
  p[f].s = p[2 * f].s + p[2 * f + 1].s;
  p[f].z = p[2 * f].z + p[2 * f + 1].z;
  update(f);
}
int query(int u, int l, int r, int flag) {
  if (r < l || r > n || l <= 0) {
    return 0;
  }
  if (p[u].l == l && p[u].r == r) {
    if (flag == 1) {
      return p[u].s;
    }
    if (flag == 0) {
      return p[u].z;
    }
  }
  if (r <= p[2 * u].r)
    return query(u * 2, l, r, flag);
  else if (l >= p[2 * u + 1].l)
    return query(2 * u + 1, l, r, flag);
  else {
    return query(2 * u, l, p[2 * u].r, flag) +
           query(2 * u + 1, p[2 * u + 1].l, r, flag);
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    f[i].n = i;
    s[i] = s[i - 1];
    if (a[i] == 1) {
      f[i].v = (n - i) - s[i - 1];
      s[i]++;
    }
    if (a[i] == 0) f[i].v = (i - 1) - s[i - 1];
  }
  for (int i = n; i >= 1; i--) {
    z[i] = z[i + 1];
    if (a[i] == 1) f[i].v -= z[i + 1];
    if (a[i] == 0) {
      f[i].v -= z[i + 1];
      z[i]++;
    }
  }
  build(1, 1, n);
  sort(f + 1, f + n + 1, cmp);
  for (int i = 1; i <= n; i++) {
    int pp = f[i].n;
    ans += query(1, 1, pp - 1, 1) + query(1, pp + 1, n, 0);
    if (a[pp] == 0) {
      p[k[pp]].z = 0;
      update(k[pp]);
    }
    if (a[pp] == 1) {
      p[k[pp]].s = 0;
      update(k[pp]);
    }
  }
  printf("%lld", ans);
  return 0;
}
