// #pragma GCC target("avx")  // CPU 処理並列化
// #pragma GCC optimize("O3")  // CPU 処理並列化
// #pragma GCC optimize("unroll-loops")  // 条件処理の呼び出しを減らす
// #define BEGIN_STACK_EXTEND(size) void * stack_extend_memory_ =
// malloc(size);void * stack_extend_origin_memory_;char *
// stack_extend_dummy_memory_ = (char*)alloca((1+(int)(((long
// long)stack_extend_memory_)&127))*16);*stack_extend_dummy_memory_ = 0;asm
// volatile("mov %%rsp, %%rbx\\nmov %%rax,
// %%rsp":"=b"(stack_extend_origin_memory_):"a"((char*)stack_extend_memory_+(size)-1024));
// #define END_STACK_EXTEND asm volatile("mov %%rax,
// %%rsp"::"a"(stack_extend_origin_memory_));free(stack_extend_memory_);

#include <algorithm>
#include <bitset>
#include <cassert>
#include <complex>
#include <deque>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
const long long mod = 1000000007;
const long long inf = mod * mod;
const long long d2 = (mod + 1) / 2;
const double EPS = 1e-11;
const double INF = 1e+10;
const double PI = acos(-1.0);
const int C_SIZE = 11100000;
const int UF_SIZE = 3100000;
namespace {
long long fact[C_SIZE];
long long finv[C_SIZE];
long long inv[C_SIZE];
inline long long Comb(int a, int b) {
  if (a < b || b < 0)
    return 0;
  return fact[a] * finv[b] % mod * finv[a - b] % mod;
}
void init_C(int n) {
  fact[0] = finv[0] = inv[1] = 1;
  for (int i = 2; i < n; i++) {
    inv[i] = (mod - (mod / i) * inv[mod % i] % mod) % mod;
  }
  for (int i = 1; i < n; i++) {
    fact[i] = fact[i - 1] * i % mod;
    finv[i] = finv[i - 1] * inv[i] % mod;
  }
}
long long pw(long long a, long long b) {
  if (a < 0LL)
    return 0;
  if (b < 0LL)
    return 0;
  long long ret = 1;
  while (b) {
    if (b % 2)
      ret = ret * a % mod;
    a = a * a % mod;
    b /= 2;
  }
  return ret;
}
long long pw_mod(long long a, long long b, long long M) {
  if (a < 0LL)
    return 0;
  if (b < 0LL)
    return 0;
  long long ret = 1;
  while (b) {
    if (b % 2)
      ret = ret * a % M;
    a = a * a % M;
    b /= 2;
  }
  return ret;
}
int pw_mod_int(int a, int b, int M) {
  if (a < 0)
    return 0;
  if (b < 0)
    return 0;
  int ret = 1;
  while (b) {
    if (b % 2)
      ret = (long long)ret * a % M;
    a = (long long)a * a % M;
    b /= 2;
  }
  return ret;
}
int ABS(int a) { return max(a, -a); }
long long ABS(long long a) { return max(a, -a); }
double ABS(double a) { return max(a, -a); }
int sig(double r) { return (r < -EPS) ? -1 : (r > +EPS) ? +1 : 0; }
int UF[UF_SIZE];
void init_UF(int n) {
  for (int i = 0; i < n; i++)
    UF[i] = -1;
}
int FIND(int a) {
  if (UF[a] < 0)
    return a;
  return UF[a] = FIND(UF[a]);
}
void UNION(int a, int b) {
  a = FIND(a);
  b = FIND(b);
  if (a == b)
    return;
  if (UF[a] > UF[b])
    swap(a, b);
  UF[a] += UF[b];
  UF[b] = a;
}
} // namespace
// ここから編集しろ
long long dp[1 << 15];
int p3[16];
int n;
int g[20][20];
int C[16][1 << 15];
long long calc(int a) {
  if (~dp[a])
    return dp[a];
  long long ret = 1;
  int t = 0;
  if (!(a & 1))
    t = 1;
  int tmp = a;
  while (tmp) {
    if (tmp != a && (tmp & (1 << t))) {
      int cnt = 0;
      for (int i = 0; i < n; i++) {
        if (!(tmp & (1 << i)))
          continue;
        cnt += C[i][a - tmp];
      }
      ret = (ret + mod - calc(tmp) * pw(d2, cnt) % mod) % mod;
    }
    tmp = (tmp - 1) & a;
  }
  // printf("%d: %lld\\n",a,ret);
  return dp[a] = ret;
}
int main() {
  int a, b;
  scanf("%d%d", &a, &b);
  n = a;
  for (int i = 0; i < b; i++) {
    int s, t;
    scanf("%d%d", &s, &t);
    s--;
    t--;
    g[s][t] = g[t][s] = 1;
  }
  for (int i = 0; i < a; i++) {
    for (int j = 0; j < (1 << a); j++) {
      for (int k = 0; k < a; k++) {
        if (j & (1 << k)) {
          if (g[i][k])
            C[i][j]++;
        }
      }
    }
  }
  long long ret = 0;
  p3[0] = 1;
  for (int i = 0; i < (1 << a); i++)
    dp[i] = -1;
  for (int i = 1; i < 16; i++)
    p3[i] = p3[i - 1] * 3;
  for (int i = 0; i < p3[a]; i++) {
    int A = 0;
    int B = 0;
    for (int j = 0; j < a; j++) {
      if (i / p3[j] % 3 == 1)
        A += (1 << j);
      if (i / p3[j] % 3 == 2)
        B += (1 << j);
    }
    if (!(A & 1))
      continue;
    if (!(B & 2))
      continue;
    bool ok = true;
    int cnt = 0;
    for (int j = 0; j < a; j++) {
      if (!(A & (1 << j)))
        continue;
      if (C[j][B])
        ok = false;
    }
    if (!ok)
      continue;
    for (int j = 0; j < a; j++) {
      if (!(A & (1 << j)))
        continue;
      cnt += C[j][(1 << a) - 1 - A];
    }
    for (int j = 0; j < a; j++) {
      if (!(B & (1 << j)))
        continue;
      cnt += C[j][(1 << a) - 1 - B];
    }
    // printf("%d: %lld\\n",i,pw(d2,cnt)*calc(A)%mod*calc(B)%mod);
    ret = (ret + pw(d2, cnt) * calc(A) % mod * calc(B)) % mod;
  }
  ret = (1 + mod - ret) % mod;
  ret = ret * pw(2, b) % mod;
  printf("%lld\\n", ret);
}

#pragma GCC target("avx")            // CPU 処理並列化
#pragma GCC optimize("O3")           // CPU 処理並列化
#pragma GCC optimize("unroll-loops") // 条件処理の呼び出しを減らす
// #define BEGIN_STACK_EXTEND(size) void * stack_extend_memory_ =
// malloc(size);void * stack_extend_origin_memory_;char *
// stack_extend_dummy_memory_ = (char*)alloca((1+(int)(((long
// long)stack_extend_memory_)&127))*16);*stack_extend_dummy_memory_ = 0;asm
// volatile("mov %%rsp, %%rbx\\nmov %%rax,
// %%rsp":"=b"(stack_extend_origin_memory_):"a"((char*)stack_extend_memory_+(size)-1024));
// #define END_STACK_EXTEND asm volatile("mov %%rax,
// %%rsp"::"a"(stack_extend_origin_memory_));free(stack_extend_memory_);

#include <algorithm>
#include <bitset>
#include <cassert>
#include <complex>
#include <deque>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
const long long mod = 1000000007;
const long long inf = mod * mod;
const long long d2 = (mod + 1) / 2;
const double EPS = 1e-11;
const double INF = 1e+10;
const double PI = acos(-1.0);
const int C_SIZE = 11100000;
const int UF_SIZE = 3100000;
namespace {
long long fact[C_SIZE];
long long finv[C_SIZE];
long long inv[C_SIZE];
inline long long Comb(int a, int b) {
  if (a < b || b < 0)
    return 0;
  return fact[a] * finv[b] % mod * finv[a - b] % mod;
}
void init_C(int n) {
  fact[0] = finv[0] = inv[1] = 1;
  for (int i = 2; i < n; i++) {
    inv[i] = (mod - (mod / i) * inv[mod % i] % mod) % mod;
  }
  for (int i = 1; i < n; i++) {
    fact[i] = fact[i - 1] * i % mod;
    finv[i] = finv[i - 1] * inv[i] % mod;
  }
}
long long pw(long long a, long long b) {
  if (a < 0LL)
    return 0;
  if (b < 0LL)
    return 0;
  long long ret = 1;
  while (b) {
    if (b % 2)
      ret = ret * a % mod;
    a = a * a % mod;
    b /= 2;
  }
  return ret;
}
long long pw_mod(long long a, long long b, long long M) {
  if (a < 0LL)
    return 0;
  if (b < 0LL)
    return 0;
  long long ret = 1;
  while (b) {
    if (b % 2)
      ret = ret * a % M;
    a = a * a % M;
    b /= 2;
  }
  return ret;
}
int pw_mod_int(int a, int b, int M) {
  if (a < 0)
    return 0;
  if (b < 0)
    return 0;
  int ret = 1;
  while (b) {
    if (b % 2)
      ret = (long long)ret * a % M;
    a = (long long)a * a % M;
    b /= 2;
  }
  return ret;
}
int ABS(int a) { return max(a, -a); }
long long ABS(long long a) { return max(a, -a); }
double ABS(double a) { return max(a, -a); }
int sig(double r) { return (r < -EPS) ? -1 : (r > +EPS) ? +1 : 0; }
int UF[UF_SIZE];
void init_UF(int n) {
  for (int i = 0; i < n; i++)
    UF[i] = -1;
}
int FIND(int a) {
  if (UF[a] < 0)
    return a;
  return UF[a] = FIND(UF[a]);
}
void UNION(int a, int b) {
  a = FIND(a);
  b = FIND(b);
  if (a == b)
    return;
  if (UF[a] > UF[b])
    swap(a, b);
  UF[a] += UF[b];
  UF[b] = a;
}
} // namespace
// ここから編集しろ
long long dp[1 << 15];
int p3[16];
int n;
int g[20][20];
int C[16][1 << 15];
long long calc(int a) {
  if (~dp[a])
    return dp[a];
  long long ret = 1;
  int t = 0;
  if (!(a & 1))
    t = 1;
  int tmp = a;
  while (tmp) {
    if (tmp != a && (tmp & (1 << t))) {
      int cnt = 0;
      for (int i = 0; i < n; i++) {
        if (!(tmp & (1 << i)))
          continue;
        cnt += C[i][a - tmp];
      }
      ret = (ret + mod - calc(tmp) * pw(d2, cnt) % mod) % mod;
    }
    tmp = (tmp - 1) & a;
  }
  // printf("%d: %lld\\n",a,ret);
  return dp[a] = ret;
}
int main() {
  int a, b;
  scanf("%d%d", &a, &b);
  n = a;
  for (int i = 0; i < b; i++) {
    int s, t;
    scanf("%d%d", &s, &t);
    s--;
    t--;
    g[s][t] = g[t][s] = 1;
  }
  for (int i = 0; i < a; i++) {
    for (int j = 0; j < (1 << a); j++) {
      for (int k = 0; k < a; k++) {
        if (j & (1 << k)) {
          if (g[i][k])
            C[i][j]++;
        }
      }
    }
  }
  long long ret = 0;
  p3[0] = 1;
  for (int i = 0; i < (1 << a); i++)
    dp[i] = -1;
  for (int i = 1; i < 16; i++)
    p3[i] = p3[i - 1] * 3;
  for (int i = 0; i < p3[a]; i++) {
    int A = 0;
    int B = 0;
    for (int j = 0; j < a; j++) {
      if (i / p3[j] % 3 == 1)
        A += (1 << j);
      if (i / p3[j] % 3 == 2)
        B += (1 << j);
    }
    if (!(A & 1))
      continue;
    if (!(B & 2))
      continue;
    bool ok = true;
    int cnt = 0;
    for (int j = 0; j < a; j++) {
      if (!(A & (1 << j)))
        continue;
      if (C[j][B])
        ok = false;
    }
    if (!ok)
      continue;
    for (int j = 0; j < a; j++) {
      if (!(A & (1 << j)))
        continue;
      cnt += C[j][(1 << a) - 1 - A];
    }
    for (int j = 0; j < a; j++) {
      if (!(B & (1 << j)))
        continue;
      cnt += C[j][(1 << a) - 1 - B];
    }
    // printf("%d: %lld\\n",i,pw(d2,cnt)*calc(A)%mod*calc(B)%mod);
    ret = (ret + pw(d2, cnt) * calc(A) % mod * calc(B)) % mod;
  }
  ret = (1 + mod - ret) % mod;
  ret = ret * pw(2, b) % mod;
  printf("%lld\\n", ret);
}


#include <algorithm>
#include <cstdio>
using namespace std;
int main() {
  int n, p[100001];
  scanf("%d", &n);
  for (int i = 0; i < 2 * n; i++)
    scanf("%d", &p[i]);
  sort(p, p + n * 2);
  double ans = 0;
  for (int i = 0; i < 2 * n; i++)
    ans += (p[i] * i) / (double)(n * 2 - 1);
  printf("%.10lf\\n", ans);
  return 0;
}

#include <algorithm>
#include <cstdio>
using namespace std;
int main() {
  long long n, p[1000001];
  scanf("%d", &n);
  for (int i = 0; i < 2 * n; i++)
    scanf("%d", &p[i]);
  sort(p, p + n * 2);
  double ans = 0;
  for (int i = 0; i < 2 * n; i++)
    ans += (p[i] * i) / (double)(n * 2 - 1);
  printf("%.10lf\\n", ans);
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
int a[111111], n;
int main() {
  scanf("%d", &n);
  n *= 2;
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  sort(a, a + n);
  double res = 0;
  for (int i = n - 1; i >= 0; i--) {
    res += a[i] / (n - 1.0) * i;
  }
  printf("%.12lf\\n", res);
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
int a[222222], n;
int main() {
  scanf("%d", &n);
  n *= 2;
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  sort(a, a + n);
  double res = 0;
  for (int i = n - 1; i >= 0; i--) {
    res += a[i] / (n - 1.0) * i;
  }
  printf("%.12lf\\n", res);
  return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#define For(i, a, b) for (i = a; i <= b; i++)
using namespace std;
inline int read() {
  int res = 0;
  bool bo = 0;
  char c;
  while (((c = getchar()) < '0' || c > '9') && c != '-')
    ;
  if (c == '-')
    bo = 1;
  else
    res = c - 48;
  while ((c = getchar()) >= '0' && c <= '9')
    res = (res << 3) + (res << 1) + (c - 48);
  return bo ? ~res + 1 : res;
}
const int N = 1e5 + 5;
int i, n, p[N];
double ans;
int main() {
  n = (read() << 1) - 1;
  For(i, 0, n) p[i] = read();
  sort(p, p + n + 1);
  For(i, 0, n) ans += 1.0 * i * p[i] / n;
  printf("%.10lf\\n", ans);
  return 0;
}

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <math.h>
#include <string>
using namespace std;

const int N = 3e5 + 5;
int n, s[N];
long long ans;

void init() {
  int i;
  scanf("%d", &n);
  for (i = 1; i <= 2 * n; ++i)
    scanf("%d", &s[i]);
  sort(s + 1, s + 2 * n + 1);
}

void work() {
  int i;
  for (i = 1; i <= 2 * n; ++i)
    ans += 1LL * (i - 1) * s[i];
  printf("%.12lf\\n", 1.0 * ans / (2 * n - 1));
}

int main() {
  init();
  work();
  return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#define For(i, a, b) for (i = a; i <= b; i++)
using namespace std;
inline int read() {
  int res = 0;
  bool bo = 0;
  char c;
  while (((c = getchar()) < '0' || c > '9') && c != '-')
    ;
  if (c == '-')
    bo = 1;
  else
    res = c - 48;
  while ((c = getchar()) >= '0' && c <= '9')
    res = (res << 3) + (res << 1) + (c - 48);
  return bo ? ~res + 1 : res;
}
const int N = 1e5 + 5;
int i, n, p[N];
double ans;
int main() {
  n = (read() << 1) - 1;
  For(i, 0, n) p[i] = read();
  sort(p, p + n + 1);
  For(i, 0, n) ans += 1.0 * i * p[i] / n;
  printf("%.10f\\n", ans);
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
long long ans, n, a[1000001];
int main() {
  cin >> n;
  for (int i = 0; i < n * 2; i++) {
    cin >> a[i];
  }
  sort(a, a + n * 2);
  for (int i = 0; i < 2 * n; i++) {
    ans += 1 * i * a[i];
  }
  printf("%.12lf\\n", 1.0 * ans / (2 * n - 1));
  return 0;
}


#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;

typedef long long ll;
ll n, p[100005];

int main() {
  scanf("%lld", &n);
  n *= 2;
  for (int i = 0; i < n; i++)
    scanf("%lld", &p[i]);
  // calc i=[0, 2n) i*pi/(2n-1)
  double x = 0;
  sort(p, p + n);
  for (int i = 0; i < n; i++)
    x += p[i] / (n - 1.) * i;
  printf("%.12lf\\n", x);
  return 0;
}

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;

typedef long long ll;
ll n, p[300005];

int main() {
  scanf("%lld", &n);
  n *= 2;
  for (int i = 0; i < n; i++)
    scanf("%lld", &p[i]);
  // calc i=[0, 2n) i*pi/(2n-1)
  double x = 0;
  sort(p, p + n);
  for (int i = 0; i < n; i++)
    x += 1.0 * p[i] / (n - 1.) * i;
  printf("%.12lf\\n", x);
  return 0;
}


#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
int n, p[100009];
long long sum = 0;
int main() {
  cin >> n;
  n *= 2;
  for (int i = 0; i < n; i++)
    cin >> p[i];
  sort(p, p + n);
  for (int i = 0; i < n; i++) {
    sum += 1LL * i * p[i];
  }
  printf("%.15Lf\\n", 1.0L * sum / (n - 1));
  return 0;
}

#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
int n, p[200009];
long long sum = 0;
int main() {
  cin >> n;
  n *= 2;
  for (int i = 0; i < n; i++)
    cin >> p[i];
  sort(p, p + n);
  for (int i = 0; i < n; i++) {
    sum += 1LL * i * p[i];
  }
  printf("%.15Lf\\n", 1.0L * sum / (n - 1));
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
long long ans, n, a[100001];
int main() {
  cin >> n;
  for (int i = 0; i < n * 2; i++) {
    cin >> a[i];
  }
  sort(a, a + n * 2);
  for (int i = 0; i < 2 * n; i++) {
    ans += i * a[i];
  }
  printf("%.12lf\\n", 1.0 * ans / (2 * n - 1));
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define _repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define _rep(i, n) _repl(i, 0, n)
#define rep(...) GET_MACRO(__VA_ARGS__, _repl, _rep)(__VA_ARGS__)
#define mp(a, b) make_pair((a), (b))
#define pb(a) push_back((a))
#define all(x) (x).begin(), (x).end()
#define uniq(x) sort(all(x)), (x).erase(unique(all(x)), end(x))
#define fi first
#define se second
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
void _dbg(string) { cout << endl; }
template <class H, class... T> void _dbg(string s, H h, T... t) {
  int l = s.find(',');
  cout << s.substr(0, l) << " = " << h << ", ";
  _dbg(s.substr(l + 1), t...);
}
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

#define INF 1120000000

int main() {
  int n;
  cin >> n;
  vector<int> p(2 * n);
  rep(i, 2 * n) cin >> p[i];

  double ans = 0;
  rep(i, 2 * n) { ans += (2 * n - i - 1.0) / (2 * n - 1.0) * p[1 * n - 1 - i]; }

  printf("%.10f\\n", ans);

  return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define _repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define _rep(i, n) _repl(i, 0, n)
#define rep(...) GET_MACRO(__VA_ARGS__, _repl, _rep)(__VA_ARGS__)
#define mp(a, b) make_pair((a), (b))
#define pb(a) push_back((a))
#define all(x) (x).begin(), (x).end()
#define uniq(x) sort(all(x)), (x).erase(unique(all(x)), end(x))
#define fi first
#define se second
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
void _dbg(string) { cout << endl; }
template <class H, class... T> void _dbg(string s, H h, T... t) {
  int l = s.find(',');
  cout << s.substr(0, l) << " = " << h << ", ";
  _dbg(s.substr(l + 1), t...);
}
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

#define INF 1120000000

int main() {
  int n;
  cin >> n;
  vector<int> p(2 * n);
  rep(i, 2 * n) cin >> p[i];
  sort(all(p));

  double ans = 0;
  rep(i, 2 * n) { ans += (2 * n - i - 1.0) / (2 * n - 1.0) * p[2 * n - 1 - i]; }

  printf("%.10f\\n", ans);

  return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#define For(i, a, b) for (i = a; i <= b; i++)
using namespace std;
inline int read() {
  int res = 0;
  bool bo = 0;
  char c;
  while (((c = getchar()) < '0' || c > '9') && c != '-')
    ;
  if (c == '-')
    bo = 1;
  else
    res = c - 48;
  while ((c = getchar()) >= '0' && c <= '9')
    res = (res << 3) + (res << 1) + (c - 48);
  return bo ? ~res + 1 : res;
}
const int N = 2e5 + 5;
int i, n, p[N];
double ans;
int main() {
  n = (read() << 1) - 1;
  For(i, 0, n) p[i] = read();
  sort(p, p + n + 1);
  For(i, 0, n) ans += 1.0 * i * p[i] / n;
  printf("%.10lf\\n", ans);
  return 0;
}


#include <algorithm>
#include <iomanip>
#include <iostream>
using namespace std;
int n, a[100009];
int main() {
  cin >> n;
  for (int i = 0; i < 2 * n; i++)
    cin >> a[i];
  sort(a, a + n);
  long long ret = 0;
  for (int i = 0; i < 2 * n; i++)
    ret += 1LL * a[i] * i;
  cout << fixed << setprecision(15) << 1.0L * ret / (2 * n - 1) << endl;
  return 0;
}

#include <algorithm>
#include <iomanip>
#include <iostream>
using namespace std;
int n, a[200009];
int main() {
  cin >> n;
  for (int i = 0; i < 2 * n; i++)
    cin >> a[i];
  sort(a, a + 2 * n);
  long long ret = 0;
  for (int i = 0; i < 2 * n; i++)
    ret += 1LL * a[i] * i;
  cout << fixed << setprecision(15) << 1.0L * ret / (2 * n - 1) << endl;
  return 0;
}


#include <bits/stdc++.h>
#define rep(i, n) for (lli i = 0; i < (n); i++)
#define rrep(i, n) for (lli i = (n)-1; i >= 0; i--)
using namespace std;
using lli = long long int;
int main() {
  double p[100005];
  int n;
  cin >> n;
  rep(i, 2 * n) cin >> p[i];
  sort(p, p + 2 * n, greater<double>());
  double sum = 0;
  rep(i, 2 * n) sum += (2.0 * n - i - 1.0) / (2.0 * n - 1.0) * p[i];
  printf("%.10f\\n", sum);
}

#include <bits/stdc++.h>
#define rep(i, n) for (lli i = 0; i < (n); i++)
#define rrep(i, n) for (lli i = (n)-1; i >= 0; i--)
using namespace std;
using lli = long long int;
int main() {
  double p[200005];
  int n;
  cin >> n;
  rep(i, 2 * n) cin >> p[i];
  sort(p, p + 2 * n, greater<double>());
  double sum = 0;
  rep(i, 2 * n) sum += (2.0 * n - i - 1.0) / (2.0 * n - 1.0) * p[i];
  printf("%.10f\\n", sum);
}


#include <bits/stdc++.h>
using namespace std;

const int MAXN = 1e5 + 5;

int N;
int a[MAXN];

int main() {
  scanf("%d", &N);
  N *= 2;
  for (int i = 0; i < N; i++)
    scanf("%d", a + i);

  sort(a, a + N);

  double sol = 0;
  for (int i = 0; i < N; i++)
    sol += (double)i * a[i];

  printf("%.10lf\\n", sol / (N - 1));

  return 0;
}

#include <bits/stdc++.h>
using namespace std;

const int MAXN = 2e5 + 5;

int N;
int a[MAXN];

int main() {
  scanf("%d", &N);
  N *= 2;
  for (int i = 0; i < N; i++)
    scanf("%d", a + i);

  sort(a, a + N);

  double sol = 0;
  for (int i = 0; i < N; i++)
    sol += (double)i * a[i];

  printf("%.10lf\\n", sol / (N - 1));

  return 0;
}


#include <bits/stdc++.h>
#define N 100005
using namespace std;
long /*double*/ long ans = 0;
long long n, a[N];
template <typename T> inline void read(T &x) {
  int f = 0;
  int orz = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      orz = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    f = (f << 3) + (f << 1) + (c & 15);
    c = getchar();
  }
  f *= orz;
  x = f;
}
template <typename T> void write(T x) {
  /*if (x==INF)
  {
      write(-1);
      return;
  }*/
  if (x < 0) {
    x = -x;
    putchar('-');
  }
  if (x >= 10)
    write(x / 10);
  putchar(x % 10 + '0');
}
int main() {
  read(n);
  n = n << 1;
  for (register int i = 1; i <= n; i++)
    read(a[i]);
  sort(a + 1, a + n + 1);
  for (register int i = 1; i <= n; i++)
    ans = ans + (unsigned long long)a[i] * (i - 1);
  /*ans/=(n-1);*/
  printf("%.13Lf", (long double)ans / (n - 1));
  putchar('\\n');
  return 0;
}

#include <bits/stdc++.h>
#define N 400005
using namespace std;
long /*double*/ long ans = 0;
long long n, a[N];
template <typename T> inline void read(T &x) {
  int f = 0;
  int orz = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      orz = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    f = (f << 3) + (f << 1) + (c & 15);
    c = getchar();
  }
  f *= orz;
  x = f;
}
template <typename T> void write(T x) {
  /*if (x==INF)
  {
      write(-1);
      return;
  }*/
  if (x < 0) {
    x = -x;
    putchar('-');
  }
  if (x >= 10)
    write(x / 10);
  putchar(x % 10 + '0');
}
int main() {
  read(n);
  n = n << 1;
  for (register int i = 1; i <= n; i++)
    read(a[i]);
  sort(a + 1, a + n + 1);
  for (register int i = 1; i <= n; i++)
    ans = ans + (unsigned long long)a[i] * (i - 1);
  /*ans/=(n-1);*/
  printf("%.13Lf", (long double)ans / (n - 1));
  putchar('\\n');
  return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define each(a, b) for (auto(a) : (b))
#define all(v) (v).begin(), (v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define cmx(x, y) x = max(x, y)
#define cmn(x, y) x = min(x, y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout << #x << " = " << (x) << endl
#define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl
#define svec(v)                                                                \\
  cout << #v << ":";                                                           \\
  rep(kbrni, v.size()) cout << " " << v[kbrni];                                \\
  cout << endl
#define sset(s)                                                                \\
  cout << #s << ":";                                                           \\
  each(kbrni, s) cout << " " << kbrni;                                         \\
  cout << endl
#define smap(m)                                                                \\
  cout << #m << ":";                                                           \\
  each(kbrni, m) cout << " {" << kbrni.first << ":" << kbrni.second << "}";    \\
  cout << endl

using namespace std;

typedef pair<int, int> P;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 100005;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vl p(n);
  rep(i, 2 * n) { cin >> p[i]; }
  sort(all(p));
  double ans = 0;
  vector<double> val(n);
  val[1] = 1;
  srep(i, 1, n + 1) {
    ans += p[i - 1] * val[i];
    val[i + 1] = val[i] * (2 * n - 2 * i) / (2 * n - 2 * i + 1);
  }
  srep(i, n + 1, 2 * n + 1) { ans += p[i - 1] * (1.0 - val[2 * n + 1 - i]); }
  printf("%.12lf\\n", ans);
  return 0;
}

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define each(a, b) for (auto(a) : (b))
#define all(v) (v).begin(), (v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define cmx(x, y) x = max(x, y)
#define cmn(x, y) x = min(x, y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout << #x << " = " << (x) << endl
#define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl
#define svec(v)                                                                \\
  cout << #v << ":";                                                           \\
  rep(kbrni, v.size()) cout << " " << v[kbrni];                                \\
  cout << endl
#define sset(s)                                                                \\
  cout << #s << ":";                                                           \\
  each(kbrni, s) cout << " " << kbrni;                                         \\
  cout << endl
#define smap(m)                                                                \\
  cout << #m << ":";                                                           \\
  each(kbrni, m) cout << " {" << kbrni.first << ":" << kbrni.second << "}";    \\
  cout << endl

using namespace std;

typedef pair<int, int> P;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 100005;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vd p(2 * n);
  rep(i, 2 * n) { cin >> p[i]; }
  sort(all(p), greater<ll>());
  double ans = 0;
  srep(i, 1, 2 * n + 1) { ans += p[i - 1] * (2 * n - i) / (2 * n - 1); }
  printf("%.10f\\n", ans);
  return 0;
}


#include <bits/stdc++.h>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<int, P> pip;
typedef vector<pip> vip;
const int inf = 1 << 30;
const ll INF = 1ll << 60;
const double pi = acos(-1);
const double eps = 1e-10;
const ll mod = 1e9 + 7;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};

int n;
vvi g;
vl F;
vvl DP, dp[3];

int dfs(int v) {
  int S = 1;
  dp[0][v][0]++;
  for (int i = 0; i < g[v].size(); i++) {
    int u = g[v][i];
    int T = dfs(u);
    S += T;
    vl a[3];
    a[0] = a[1] = a[2] = vl(n);
    a[0][0]++;
    if (!i) {
      a[0] = DP[u];
      for (int j = 1; j < S; j++)
        a[1][j] = (dp[0][u][j - 1] * 2 + dp[1][u][j - 1]) % mod;
    } else {
      for (int j = 0; j < 3; j++)
        for (int k = 1; k < S; k++)
          for (int l = max(0, k - T); l <= k; l++) {
            (a[j][k] += dp[j][v][l] * DP[u][k - l]) %= mod;
            if (j && l < k)
              (a[j][k] +=
               dp[j - 1][v][l] *
               (dp[0][u][k - l - 1] * (3 - j) +
                dp[1][u][k - l - 1] * (j == 1 ? 1 : mod / 2 + 1) % mod)) %= mod;
          }
    }
    dp[0][v] = a[0];
    dp[1][v] = a[1];
    dp[2][v] = a[2];
  }
  for (int i = 0; i < 3; i++)
    for (int j = 0; j < S; j++)
      (DP[v][j] += dp[i][v][j]) %= mod;
  return S;
}

int main() {
  cin >> n;
  g = vvi(n);
  F = vl(n + 1, 1);
  DP = dp[0] = dp[1] = dp[2] = vvl(n, vl(n));
  for (int i = 1; i < n; i++) {
    int v;
    cin >> v;
    g[v - 1].push_back(i);
  }
  dfs(0);
  ll res = 0;
  for (int i = 1; i <= n; i++)
    F[i] = F[i - 1] * i % mod;
  for (int i = 0; i < n; i++) {
    ll tmp = F[n - i] * DP[0][i] % mod;
    if (i % 2 == 1)
      tmp = (mod - tmp) % mod;
    (res += tmp) %= mod;
  }
  cout << res << endl;
}

#include <bits/stdc++.h>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<int, P> pip;
typedef vector<pip> vip;
const int inf = 1 << 30;
const ll INF = 1ll << 60;
const double pi = acos(-1);
const double eps = 1e-10;
const ll mod = 1e9 + 7;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};

int n;
vvi g;
vl F;
vvl DP, dp[3];

int dfs(int v) {
  int S = 1;
  dp[0][v][0]++;
  vp b;
  for (int i = 0; i < g[v].size(); i++) {
    int u = g[v][i];
    b.push_back({dfs(u), u});
  }
  sort(b.rbegin(), b.rend());
  for (int i = 0; i < b.size(); i++) {
    int T = b[i].first, u = b[i].second;
    S += T;
    vl a[3];
    a[0] = a[1] = a[2] = vl(n);
    a[0][0]++;
    if (!i) {
      a[0] = DP[u];
      for (int j = 1; j < S; j++)
        a[1][j] = (dp[0][u][j - 1] * 2 + dp[1][u][j - 1]) % mod;
    } else {
      for (int j = 0; j < 3; j++)
        for (int k = 1; k < S; k++)
          for (int l = max(0, k - T); l <= k; l++) {
            (a[j][k] += dp[j][v][l] * DP[u][k - l]) %= mod;
            if (j && l < k)
              (a[j][k] +=
               dp[j - 1][v][l] *
               (dp[0][u][k - l - 1] * (3 - j) +
                dp[1][u][k - l - 1] * (j == 1 ? 1 : mod / 2 + 1) % mod)) %= mod;
          }
    }
    dp[0][v] = a[0];
    dp[1][v] = a[1];
    dp[2][v] = a[2];
  }
  for (int i = 0; i < 3; i++)
    for (int j = 0; j < S; j++)
      (DP[v][j] += dp[i][v][j]) %= mod;
  return S;
}

int main() {
  cin >> n;
  g = vvi(n);
  F = vl(n + 1, 1);
  DP = dp[0] = dp[1] = dp[2] = vvl(n, vl(n));
  for (int i = 1; i < n; i++) {
    int v;
    cin >> v;
    g[v - 1].push_back(i);
  }
  dfs(0);
  ll res = 0;
  for (int i = 1; i <= n; i++)
    F[i] = F[i - 1] * i % mod;
  for (int i = 0; i < n; i++) {
    ll tmp = F[n - i] * DP[0][i] % mod;
    if (i % 2 == 1)
      tmp = (mod - tmp) % mod;
    (res += tmp) %= mod;
  }
  cout << res << endl;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int MAXN = 2010;
const ll MOD = 1000000007;

inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); ch = getchar())
    x = (x * 10) + (ch ^ 48);
  return x * f;
}

inline ll qpow(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1LL)
      res = res * a % MOD;
    b >>= 1, a = a * a % MOD;
  }
  return res;
}

int e, st[MAXN], to[MAXN];
int nxt[MAXN];
inline void Add(int u, int v) {
  to[++e] = v, nxt[e] = st[u];
  st[u] = e;
}

int n, fa[MAXN], size[MAXN];
ll dp[MAXN][MAXN][3], inv2;
ll tmp[MAXN][3];
ll fac[MAXN], ans;

inline void update(ll &cur, ll val) {
  cur = cur + val;
  if (cur >= MOD)
    cur -= MOD;
}

void dfs(int u) {
  int i, j, k;
  dp[u][1][0] = 1, size[u] = 1;
  for (i = st[u]; i; i = nxt[i]) {
    int v = to[i];
    dfs(v), size[u] += size[v];
  }
  if (size[u] == 1)
    return;
  for (i = st[u]; i; i = nxt[i]) {
    for (k = size[u]; k >= 1; k--) {
      tmp[k][0] = dp[u][k][0];
      tmp[k][1] = dp[u][k][1];
      tmp[k][2] = dp[u][k][2];
      dp[u][k][0] = dp[u][k][1] = dp[u][k][2] = 0;
    }
    int v = to[i];
    for (k = size[u]; k >= 1; k--) {
      for (j = min(size[v], size[u] - k + 1); j >= 1; j--) {
        if (j + k - 1 != size[u]) {
          update(dp[u][j + k][0], dp[v][j][0] * tmp[k][0] % MOD);
          update(dp[u][j + k][1], dp[v][j][0] * tmp[k][1] % MOD);
          update(dp[u][j + k][2], dp[v][j][0] * tmp[k][2] % MOD);
          update(dp[u][j + k][0], dp[v][j][1] * tmp[k][0] % MOD);
          update(dp[u][j + k][1], dp[v][j][1] * tmp[k][1] % MOD);
          update(dp[u][j + k][2], dp[v][j][1] * tmp[k][2] % MOD);
          update(dp[u][j + k][0], dp[v][j][2] * tmp[k][0] % MOD);
          update(dp[u][j + k][1], dp[v][j][2] * tmp[k][1] % MOD);
          update(dp[u][j + k][2], dp[v][j][2] * tmp[k][2] % MOD);
        }
        update(dp[u][j + k - 1][2], dp[v][j][0] * tmp[k][1] % MOD);
        update(dp[u][j + k - 1][2], dp[v][j][1] * tmp[k][1] % MOD * inv2 % MOD);
        update(dp[u][j + k - 1][1], dp[v][j][0] * tmp[k][0] % MOD * 2 % MOD);
        update(dp[u][j + k - 1][1], dp[v][j][1] * tmp[k][0] % MOD);
      }
    }
    /*if(u == 1) {
            printf(":");
            for(k = 1; k <= size[u]; k++)
                    printf("(%lld,%lld,%lld) ", dp[u][k][0], dp[u][k][1],
    dp[u][k][2]); printf("\\n");
    }*/
  }
  /*for(k = 1; k <= size[u]; k++)
          printf("(%lld,%lld,%lld) ", dp[u][k][0], dp[u][k][1], dp[u][k][2]);
  printf("\\n");*/
}

int main() {
  int i;

  n = read();
  for (i = 2; i <= n; i++)
    Add(read(), i);

  inv2 = qpow(2, MOD - 2);
  dfs(1);
  // printf("%lld\\n", dp[4][1][0]);
  // printf("%lld\\n", dp[3][1][1]);
  // printf("%lld\\n", dp[3][2][0]);

  fac[0] = 1;
  for (i = 1; i <= n; i++)
    fac[i] = fac[i - 1] * i % MOD;
  for (i = 0; i < n; i++) {
    int f = i & 1 ? MOD - 1 : 1;
    update(ans,
           f * fac[n - i] % MOD *
               ((dp[1][n - i][0] + dp[1][n - i][1] + dp[1][n - i][2]) % MOD) %
               MOD);
  }
  printf("%lld\\n", ans);
  return 0;
}

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int MAXN = 2010;
const ll MOD = 1000000007;

inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); ch = getchar())
    x = (x * 10) + (ch ^ 48);
  return x * f;
}

inline ll qpow(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1LL)
      res = res * a % MOD;
    b >>= 1, a = a * a % MOD;
  }
  return res;
}

int e, st[MAXN], to[MAXN];
int nxt[MAXN];
inline void Add(int u, int v) {
  to[++e] = v, nxt[e] = st[u];
  st[u] = e;
}

int n, fa[MAXN], size[MAXN];
ll dp[MAXN][MAXN][3], inv2;
ll tmp[MAXN][3];
ll fac[MAXN], ans;

inline void update(ll &cur, ll val) {
  cur = cur + val;
  if (cur >= MOD)
    cur -= MOD;
}

void dfs(int u) {
  int i, j, k;
  dp[u][1][0] = 1, size[u] = 1;
  for (i = st[u]; i; i = nxt[i]) {
    int v = to[i];
    dfs(v), size[u] += size[v];
  }
  if (size[u] == 1)
    return;
  for (i = st[u]; i; i = nxt[i]) {
    for (k = size[u]; k >= 1; k--) {
      tmp[k][0] = dp[u][k][0];
      tmp[k][1] = dp[u][k][1];
      tmp[k][2] = dp[u][k][2];
      dp[u][k][0] = dp[u][k][1] = dp[u][k][2] = 0;
    }
    int v = to[i];
    for (k = size[u] - size[v] + 1; k >= 1; k--) {
      for (j = size[v]; j >= 1; j--) {
        if (j + k - 1 != size[u]) {
          update(dp[u][j + k][0], dp[v][j][0] * tmp[k][0] % MOD);
          update(dp[u][j + k][1], dp[v][j][0] * tmp[k][1] % MOD);
          update(dp[u][j + k][2], dp[v][j][0] * tmp[k][2] % MOD);
          update(dp[u][j + k][0], dp[v][j][1] * tmp[k][0] % MOD);
          update(dp[u][j + k][1], dp[v][j][1] * tmp[k][1] % MOD);
          update(dp[u][j + k][2], dp[v][j][1] * tmp[k][2] % MOD);
          update(dp[u][j + k][0], dp[v][j][2] * tmp[k][0] % MOD);
          update(dp[u][j + k][1], dp[v][j][2] * tmp[k][1] % MOD);
          update(dp[u][j + k][2], dp[v][j][2] * tmp[k][2] % MOD);
        }
        update(dp[u][j + k - 1][2], dp[v][j][0] * tmp[k][1] % MOD);
        update(dp[u][j + k - 1][2], dp[v][j][1] * tmp[k][1] % MOD * inv2 % MOD);
        update(dp[u][j + k - 1][1], dp[v][j][0] * tmp[k][0] % MOD * 2 % MOD);
        update(dp[u][j + k - 1][1], dp[v][j][1] * tmp[k][0] % MOD);
      }
    }
    /*if(u == 1) {
            printf(":");
            for(k = 1; k <= size[u]; k++)
                    printf("(%lld,%lld,%lld) ", dp[u][k][0], dp[u][k][1],
    dp[u][k][2]); printf("\\n");
    }*/
  }
  /*for(k = 1; k <= size[u]; k++)
          printf("(%lld,%lld,%lld) ", dp[u][k][0], dp[u][k][1], dp[u][k][2]);
  printf("\\n");*/
}

int main() {
  int i;

  n = read();
  for (i = 2; i <= n; i++)
    Add(read(), i);

  inv2 = qpow(2, MOD - 2);
  dfs(1);
  // printf("%lld\\n", dp[4][1][0]);
  // printf("%lld\\n", dp[3][1][1]);
  // printf("%lld\\n", dp[3][2][0]);

  fac[0] = 1;
  for (i = 1; i <= n; i++)
    fac[i] = fac[i - 1] * i % MOD;
  for (i = 0; i < n; i++) {
    int f = i & 1 ? MOD - 1 : 1;
    update(ans,
           f * fac[n - i] % MOD *
               ((dp[1][n - i][0] + dp[1][n - i][1] + dp[1][n - i][2]) % MOD) %
               MOD);
  }
  printf("%lld\\n", ans);
  return 0;
}


#include <bits/stdc++.h>
#define show(x) std::cerr << #x << " = " << (x) << std::endl
using ll = long long;
template <typename T> constexpr T MOD = static_cast<T>(1000000007);
template <typename T> constexpr T INF = std::numeric_limits<T>::max() / 16;
template <typename T, T mod = MOD<T>> class ModInt {
private:
  T value;
  static T pow(const T p, const T n) {
    return p < 0        ? pow(mod + p, n)
           : n == 0     ? 1
           : n % 2 == 1 ? pow(p, n - 1) * p % mod
                        : pow(p * p % mod, n / 2);
  }
  static T inv(const T p) { return pow(p, mod - 2); }

public:
  ModInt() : value{0} {}
  ModInt(const T val) : value{((val % mod) + mod) % mod} {}
  ModInt(const ModInt<T, mod> &n) : value{n()} {}
  ModInt<T, mod> &operator=(const ModInt<T, mod> &n) {
    return value = n(), (*this);
  }
  ModInt<T, mod> &operator=(const T v) {
    return value = (mod + v % mod) % mod, (*this);
  }
  ModInt<T, mod> operator+() const { return *this; }
  ModInt<T, mod> operator-() const { return ModInt{mod - value}; }
  ModInt<T, mod> operator+(const ModInt<T, mod> &val) const {
    return ModInt{value + val()};
  }
  ModInt<T, mod> operator-(const ModInt<T, mod> &val) const {
    return ModInt{value - val() + mod};
  }
  ModInt<T, mod> operator*(const ModInt<T, mod> &val) const {
    return ModInt{value * val()};
  }
  ModInt<T, mod> operator/(const ModInt<T, mod> &val) const {
    return ModInt{value * inv(val())};
  }
  ModInt<T, mod> &operator+=(const ModInt<T, mod> &val) {
    return (((value += val()) %= mod) += mod) %= mod, (*this);
  }
  ModInt<T, mod> &operator-=(const ModInt<T, mod> &val) {
    return (((value -= val()) %= mod) += mod) %= mod, (*this);
  }
  ModInt<T, mod> &operator*=(const ModInt<T, mod> &val) {
    return (((value *= val()) %= mod) += mod) %= mod, (*this);
  }
  ModInt<T, mod> &operator/=(const ModInt<T, mod> &val) {
    return (((value *= inv(val())) %= mod) += mod) %= mod, (*this);
  }
  ModInt<T, mod> operator+(const T val) const { return ModInt{value + val}; }
  ModInt<T, mod> operator-(const T val) const { return ModInt{value - val}; }
  ModInt<T, mod> operator*(const T val) const { return ModInt{value * val}; }
  ModInt<T, mod> operator/(const T val) const {
    return ModInt{value * inv(val)};
  }
  ModInt<T, mod> &operator+=(const T val) {
    return (((value += val) %= mod) += mod) %= mod, (*this);
  }
  ModInt<T, mod> &operator-=(const T val) {
    return (((value -= val) %= mod) += mod) %= mod, (*this);
  }
  ModInt<T, mod> &operator*=(const T val) {
    return (((value *= val) %= mod) += mod) %= mod, (*this);
  }
  ModInt<T, mod> &operator/=(const T val) {
    return (((value *= inv(val)) %= mod) += mod) %= mod, (*this);
  }
  bool operator==(const ModInt<T, mod> &val) const {
    return value == val.value;
  }
  bool operator!=(const ModInt<T, mod> &val) const { return not(*this == val); }
  bool operator==(const T val) const {
    return value == (mod + val % mod) % mod;
  }
  bool operator!=(const T val) const { return not(*this == val); }
  T operator()() const { return value; }
};
template <typename T, T mod>
inline ModInt<T, mod> operator+(const T val, const ModInt<T, mod> &n) {
  return ModInt<T, mod>{n() + val};
}
template <typename T, T mod>
inline ModInt<T, mod> operator-(const T val, const ModInt<T, mod> &n) {
  return ModInt<T, mod>{-n() + val};
}
template <typename T, T mod>
inline ModInt<T, mod> operator*(const T val, const ModInt<T, mod> &n) {
  return ModInt<T, mod>{n() * val};
}
template <typename T, T mod>
inline ModInt<T, mod> operator/(const T val, const ModInt<T, mod> &n) {
  return ModInt<T, mod>(val) / n;
}
template <typename T, T mod>
inline bool operator==(const T val, const ModInt<T, mod> &n) {
  return n == val;
}
template <typename T, T mod>
inline bool operator!=(const T val, const ModInt<T, mod> &n) {
  return not(val == n);
}
template <typename T, T mod>
inline std::istream &operator>>(std::istream &is, ModInt<T, mod> &n) {
  T v;
  return is >> v, n = v, is;
}
template <typename T, T mod>
std::ostream &operator<<(std::ostream &os, const ModInt<T, mod> &n) {
  return (os << n());
}
using mint = ModInt<int, MOD<int>>;
using mll = ModInt<ll, MOD<ll>>;
using Poly = std::vector<mll>;
Poly operator*(const Poly &p1, const Poly &p2) {
  const int d1 = p1.size() - 1, d2 = p2.size() - 1, d = d1 + d2;
  Poly ans(d, 0);
  for (int i = 0; i <= d1; i++) {
    for (int j = 0; j < d2; j++) {
      ans[i + j] += p1[i] * p2[j];
    }
  }
  return ans;
}
Poly operator*(const ll s, const Poly &p2) {
  const int d = p2.size() - 1;
  Poly ans(d + 1, 0);
  for (int i = 0; i <= d; i++) {
    ans[i] = p2[i] * d;
  }
  return ans;
}
Poly operator+(const Poly &p1, const Poly &p2) {
  const int d = p1.size() - 1;
  assert(p2.size() == d + 1);
  Poly ans(d + 1, 0);
  for (int i = 0; i <= d; i++) {
    ans[i] = p1[i] + p2[i];
  }
  return ans;
}

int main() {
  int N;
  std::cin >> N;
  std::vector<std::vector<int>> g(N);
  for (int i = 1, p; i < N; i++) {
    std::cin >> p, g[p - 1].push_back(i);
  }
  // vi以下のパス分割全体で、根viの次数がj, 連結成分数k
  // 2^{連結成分数}/2^{孤立点数} の和
  std::vector<std::vector<Poly>> dp(N,
                                    std::vector<std::vector<mll>>(3, Poly{1}));
  auto dfs = [&](auto &&self, const int s) -> void {
    for (const int to : g[s]) {
      self(self, to);
    }
    Poly p0{1}, p1{1}, p2{1};
    for (const int to : g[s]) {
      const Poly z = dp[to][0] + dp[to][1] + dp[to][2];
      const Poly o = 2 * dp[to][0] + dp[to][1];
      p0 = p0 * z;
      p1 = p1 * z + p0 * o;
      p2 = p2 * z + p1 * o;
    }
    dp[s][0] = p0 * Poly{0, 1};
    dp[s][1] = p1;
    for (int i = 1; i < p2.size(); i++) {
      p2[i - 1] = p2[i] * (MOD<ll> + 1) / 2;
    }
    dp[s][2] = p2;
  };
  dfs(dfs, 0);
  mll p = 1, ans = 0;
  const Poly f = dp[0][0] + dp[0][1] + dp[0][2];
  for (int i = 0; i <= N; i++) {
    ans += (i % 2 == 0 ? p * f[i] : (-p) * f[i]);
    p *= (i + 1);
  }
  std::cout << ans << std::endl;
  return 0;
}

#include <bits/stdc++.h>
#define show(x) std::cerr << #x << " = " << (x) << std::endl
using ll = long long;
template <typename T> constexpr T MOD = static_cast<T>(1000000007);
template <typename T> constexpr T INF = std::numeric_limits<T>::max() / 16;
template <typename T, T mod = MOD<T>> class ModInt {
private:
  T value;
  static T pow(const T p, const T n) {
    return p < 0        ? pow(mod + p, n)
           : n == 0     ? 1
           : n % 2 == 1 ? pow(p, n - 1) * p % mod
                        : pow(p * p % mod, n / 2);
  }
  static T inv(const T p) { return pow(p, mod - 2); }

public:
  ModInt() : value{0} {}
  ModInt(const T val) : value{((val % mod) + mod) % mod} {}
  ModInt(const ModInt<T, mod> &n) : value{n()} {}
  ModInt<T, mod> &operator=(const ModInt<T, mod> &n) {
    return value = n(), (*this);
  }
  ModInt<T, mod> &operator=(const T v) {
    return value = (mod + v % mod) % mod, (*this);
  }
  ModInt<T, mod> operator+() const { return *this; }
  ModInt<T, mod> operator-() const { return ModInt{mod - value}; }
  ModInt<T, mod> operator+(const ModInt<T, mod> &val) const {
    return ModInt{value + val()};
  }
  ModInt<T, mod> operator-(const ModInt<T, mod> &val) const {
    return ModInt{value - val() + mod};
  }
  ModInt<T, mod> operator*(const ModInt<T, mod> &val) const {
    return ModInt{value * val()};
  }
  ModInt<T, mod> operator/(const ModInt<T, mod> &val) const {
    return ModInt{value * inv(val())};
  }
  ModInt<T, mod> &operator+=(const ModInt<T, mod> &val) {
    return (((value += val()) %= mod) += mod) %= mod, (*this);
  }
  ModInt<T, mod> &operator-=(const ModInt<T, mod> &val) {
    return (((value -= val()) %= mod) += mod) %= mod, (*this);
  }
  ModInt<T, mod> &operator*=(const ModInt<T, mod> &val) {
    return (((value *= val()) %= mod) += mod) %= mod, (*this);
  }
  ModInt<T, mod> &operator/=(const ModInt<T, mod> &val) {
    return (((value *= inv(val())) %= mod) += mod) %= mod, (*this);
  }
  ModInt<T, mod> operator+(const T val) const { return ModInt{value + val}; }
  ModInt<T, mod> operator-(const T val) const { return ModInt{value - val}; }
  ModInt<T, mod> operator*(const T val) const { return ModInt{value * val}; }
  ModInt<T, mod> operator/(const T val) const {
    return ModInt{value * inv(val)};
  }
  ModInt<T, mod> &operator+=(const T val) {
    return (((value += val) %= mod) += mod) %= mod, (*this);
  }
  ModInt<T, mod> &operator-=(const T val) {
    return (((value -= val) %= mod) += mod) %= mod, (*this);
  }
  ModInt<T, mod> &operator*=(const T val) {
    return (((value *= val) %= mod) += mod) %= mod, (*this);
  }
  ModInt<T, mod> &operator/=(const T val) {
    return (((value *= inv(val)) %= mod) += mod) %= mod, (*this);
  }
  bool operator==(const ModInt<T, mod> &val) const {
    return value == val.value;
  }
  bool operator!=(const ModInt<T, mod> &val) const { return not(*this == val); }
  bool operator==(const T val) const {
    return value == (mod + val % mod) % mod;
  }
  bool operator!=(const T val) const { return not(*this == val); }
  T operator()() const { return value; }
};
template <typename T, T mod>
inline ModInt<T, mod> operator+(const T val, const ModInt<T, mod> &n) {
  return ModInt<T, mod>{n() + val};
}
template <typename T, T mod>
inline ModInt<T, mod> operator-(const T val, const ModInt<T, mod> &n) {
  return ModInt<T, mod>{-n() + val};
}
template <typename T, T mod>
inline ModInt<T, mod> operator*(const T val, const ModInt<T, mod> &n) {
  return ModInt<T, mod>{n() * val};
}
template <typename T, T mod>
inline ModInt<T, mod> operator/(const T val, const ModInt<T, mod> &n) {
  return ModInt<T, mod>(val) / n;
}
template <typename T, T mod>
inline bool operator==(const T val, const ModInt<T, mod> &n) {
  return n == val;
}
template <typename T, T mod>
inline bool operator!=(const T val, const ModInt<T, mod> &n) {
  return not(val == n);
}
template <typename T, T mod>
inline std::istream &operator>>(std::istream &is, ModInt<T, mod> &n) {
  T v;
  return is >> v, n = v, is;
}
template <typename T, T mod>
std::ostream &operator<<(std::ostream &os, const ModInt<T, mod> &n) {
  return (os << n());
}
using mint = ModInt<int, MOD<int>>;
using mll = ModInt<ll, MOD<ll>>;
using Poly = std::vector<mll>;
Poly operator*(const Poly &p1, const Poly &p2) {
  const int d1 = p1.size() - 1, d2 = p2.size() - 1, d = d1 + d2;
  Poly ans(d + 1, 0);
  for (int i = 0; i <= d1; i++) {
    for (int j = 0; j <= d2; j++) {
      ans[i + j] += p1[i] * p2[j];
    }
  }
  return ans;
}
Poly operator*(const ll s, const Poly &p2) {
  const int d = p2.size() - 1;
  Poly ans(d + 1, 0);
  for (int i = 0; i <= d; i++) {
    ans[i] = p2[i] * s;
  }
  return ans;
}
Poly operator+(const Poly &p1, const Poly &p2) {
  const int d1 = p1.size() - 1, d2 = p2.size() - 1, d = std::max(d1, d2);
  Poly ans(d + 1, 0);
  for (int i = 0; i <= d1; i++) {
    ans[i] += p1[i];
  }
  for (int i = 0; i <= d2; i++) {
    ans[i] += p2[i];
  }
  return ans;
}
template <typename T, typename A>
std::ostream &operator<<(std::ostream &os, const std::vector<T, A> &v) {
  os << "[";
  for (const auto &e : v) {
    os << e << ",";
  }
  return (os << "]" << std::endl);
}
int main() {
  int N;
  std::cin >> N;
  std::vector<std::vector<int>> g(N);
  for (int i = 1, p; i < N; i++) {
    std::cin >> p, g[p - 1].push_back(i);
  }
  // vi以下のパス分割全体で、根viの次数がj, 連結成分数k
  // 2^{連結成分数}/2^{孤立点数} の和
  std::vector<std::vector<Poly>> dp(N, std::vector<std::vector<mll>>(3));
  auto dfs = [&](auto &&self, const int s) -> void {
    for (const int to : g[s]) {
      self(self, to);
    }
    Poly p0{1}, p1{0}, p2{0};
    for (const int to : g[s]) {
      const Poly z = dp[to][0] + dp[to][1] + dp[to][2];
      const Poly o = 2 * dp[to][0] + dp[to][1];
      auto p0_ = p0, p1_ = p1, p2_ = p2;
      p0 = p0_ * z, p1 = p1_ * z + p0_ * o, p2 = p2_ * z + p1_ * o;
    }
    // show(s);
    // show(p0), show(p1), show(p2);
    dp[s][0] = p0 * Poly{0, 1};
    dp[s][1] = p1;
    for (int i = 1; i < p2.size(); i++) {
      p2[i - 1] = p2[i] * (MOD<ll> + 1) / 2;
    }
    p2[p2.size() - 1] = 0;
    dp[s][2] = p2;
  };
  dfs(dfs, 0);
  //    show(dp);
  mll p = 1, ans = 0;
  const Poly f = dp[0][0] + dp[0][1] + dp[0][2];
  for (int i = 0; i <= N; i++) {
    ans += ((N - i) % 2 == 0 ? p * f[i] : (-p) * f[i]);
    p *= (i + 1);
  }
  std::cout << ans << std::endl;
  return 0;
}

terminate called after throwing an instance of 'std::length_error'
  what():  cannot create std::vector larger than max_size()


#include <algorithm>
#include <bitset>
#include <cassert>
#include <deque>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
const long long mod = 1000000007;
const long long inf = mod * mod;
const long long d2 = 500000004;
const double EPS = 1e-6;
const double PI = acos(-1.0);
int ABS(int a) { return max(a, -a); }
long long ABS(long long a) { return max(a, -a); }
vector<int> g[2100];
int sz[2100];
long long dp[2][2100];
long long dp2[3][2100][2100];
long long fact[3100];
void solve(int a) {
  int n = g[a].size();
  dp2[0][0][0] = 1;
  for (int i = 0; i < g[a].size(); i++) {
    solve(g[a][i]);
    int T = sz[a];
    sz[a] += sz[g[a][i]];
    for (int j = 0; j <= sz[a]; j++) {
      dp2[0][i + 1][j] = dp2[1][i + 1][j] = dp2[2][i + 1][j] = 0;
    }
    for (int j = 0; j <= T; j++) {
      for (int k = 0; k <= sz[g[a][i]]; k++) {
        dp2[0][i + 1][j + k] =
            (dp2[0][i + 1][j + k] + dp2[0][i][j] * dp[0][k]) % mod;
        dp2[1][i + 1][j + k] =
            (dp2[0][i + 1][j + k] + dp2[0][i][j] * dp[1][k]) % mod;
        dp2[1][i + 1][j + k] =
            (dp2[0][i + 1][j + k] + dp2[1][i][j] * dp[0][k]) % mod;
        dp2[2][i + 1][j + k] =
            (dp2[0][i + 1][j + k] + dp2[1][i][j] * dp[1][k]) % mod;
        dp2[2][i + 1][j + k] =
            (dp2[0][i + 1][j + k] + dp2[2][i][j] * dp[0][k]) % mod;
      }
    }
  }
  for (int i = 0; i <= sz[g[a][i]]; i++) {
    dp[0][i] = (dp2[0][n][i] + dp2[1][n][i] * 2 + dp2[2][n][i]) % mod;
    dp[1][i + 1] = dp2[1][n][i] % mod;
  }
  sz[a]++;
}
int main() {
  int a;
  scanf("%d", &a);
  for (int i = 0; i < a - 1; i++) {
    int p;
    scanf("%d", &p);
    p--;
    g[p].push_back(i + 1);
  }
  solve(0);
  long long ret = 0;
  fact[0] = 1;
  for (int i = 1; i < 3100; i++)
    fact[i] = fact[i - 1] * i % mod;

  for (int i = 0; i <= a; i++) {
    if (i % 2 == 0)
      ret = (ret + dp[0][i] * fact[a - i] % mod) % mod;
    else
      ret = (ret + mod - dp[0][i] * fact[a - i] % mod) % mod;
  }
  printf("%lld\\n", ret);
}


// https://cf17-exhibition-open.contest.atcoder.jp/tasks/cf17_exhibition_b
#include <algorithm>
#include <cstdio>
using namespace std;
#define N 200020
#define inf 0x7fffffff
#define ll long long
int n, mn[N], add[N], b[N], a[N];
ll ans = 0;
inline void pushup(int x) { mn[x] = min(mn[x << 1], mn[x << 1 | 1]); }
inline void pushdown(int x) {
  mn[x << 1] += add[x];
  mn[x << 1 | 1] += add[x];
  add[x << 1] += add[x];
  add[x << 1 | 1] += add[x];
  add[x] = 0;
}
void build(int v, int l, int r) {
  if (l == r) {
    mn[v] = b[l];
    return;
  }
  int mid = l + r >> 1;
  build(v << 1, l, mid);
  build(v << 1 | 1, mid + 1, r);
  pushup(v);
}
void ins(int v, int l, int r, int x, int y) {
  if (x > y)
    return;
  if (x <= l && r <= y) {
    add[v]++;
    mn[v]++;
    return;
  }
  int mid = l + r >> 1;
  pushdown(v);
  if (x <= mid)
    ins(v << 1, l, mid, x, y);
  if (mid < y)
    ins(v << 1 | 1, mid + 1, r, x, y);
  pushup(v);
}
int query(int v, int l, int r, int x, int y) {
  if (x <= l && r <= y)
    return mn[v];
  int mid = l + r >> 1, s = inf;
  pushdown(v);
  if (x <= mid)
    s = min(s, query(v << 1, l, mid, x, y));
  if (mid < y)
    s = min(s, query(v << 1 | 1, mid + 1, r, x, y));
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]), b[i] = a[i];
  sort(b + 1, b + n + 1);
  int nn = unique(b + 1, b + n + 1) - b - 1;
  build(1, 1, nn);
  for (int i = 1; i <= n; i++) {
    int p = lower_bound(b + 1, b + nn + 1, a[i]) - b;
    ans += query(1, 1, nn, p, nn) - a[i];
    ins(1, 1, nn, 1, p - 1);
  }
  printf("%lld\\n", ans);
  return 0;
}

// https://cf17-exhibition-open.contest.atcoder.jp/tasks/cf17_exhibition_b
#include <algorithm>
#include <cstdio>
using namespace std;
#define N 200020
#define inf 0x7fffffff
#define ll long long
int n, mn[N << 2], add[N << 2], b[N], a[N];
ll ans = 0;
inline void pushup(int x) { mn[x] = min(mn[x << 1], mn[x << 1 | 1]); }
inline void pushdown(int x) {
  mn[x << 1] += add[x];
  mn[x << 1 | 1] += add[x];
  add[x << 1] += add[x];
  add[x << 1 | 1] += add[x];
  add[x] = 0;
}
void build(int v, int l, int r) {
  if (l == r) {
    mn[v] = b[l];
    return;
  }
  int mid = l + r >> 1;
  build(v << 1, l, mid);
  build(v << 1 | 1, mid + 1, r);
  pushup(v);
}
void ins(int v, int l, int r, int x, int y) {
  if (x > y)
    return;
  if (x <= l && r <= y) {
    add[v]++;
    mn[v]++;
    return;
  }
  int mid = l + r >> 1;
  pushdown(v);
  if (x <= mid)
    ins(v << 1, l, mid, x, y);
  if (mid < y)
    ins(v << 1 | 1, mid + 1, r, x, y);
  pushup(v);
}
int query(int v, int l, int r, int x, int y) {
  if (x <= l && r <= y)
    return mn[v];
  int mid = l + r >> 1, s = inf;
  pushdown(v);
  if (x <= mid)
    s = min(s, query(v << 1, l, mid, x, y));
  if (mid < y)
    s = min(s, query(v << 1 | 1, mid + 1, r, x, y));
  return s;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]), b[i] = a[i];
  sort(b + 1, b + n + 1);
  int nn = unique(b + 1, b + n + 1) - b - 1;
  build(1, 1, nn);
  for (int i = 1; i <= n; i++) {
    int p = lower_bound(b + 1, b + nn + 1, a[i]) - b;
    ans += query(1, 1, nn, p, nn) - a[i];
    ins(1, 1, nn, 1, p - 1);
  }
  printf("%lld\\n", ans);
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int N = 1e5 + 5;
int n, m, a[N], b[N], tree[4 * N], tree2[4 * N], lazy[4 * N];
ll ans;
void initseg(int nd, int l, int r) {
  if (l == r) {
    tree[nd] = b[l];
    tree2[nd] = l;
    return;
  }
  int m = (l + r) / 2;
  initseg(nd * 2, l, m);
  initseg(nd * 2 + 1, m + 1, r);
  tree[nd] = tree[nd * 2];
  tree2[nd] = l;
}
void pushdown(int nd, int l, int r) {
  if (l != r) {
    lazy[nd * 2] += lazy[nd];
    lazy[nd * 2 + 1] += lazy[nd];
  }
  tree[nd] += lazy[nd];
  lazy[nd] = 0;
}
P query(int nd, int l, int r, int s, int e) {
  if (r < s || e < l)
    return P(2e9, 2e9);
  pushdown(nd, l, r);
  if (s <= l && r <= e)
    return P(tree[nd], tree2[nd]);
  int m = (l + r) / 2;
  P p1 = query(nd * 2, l, m, s, e);
  P p2 = query(nd * 2 + 1, m + 1, r, s, e);
  if (p1.first > p2.first)
    return p2;
  return p1;
}
void update(int nd, int l, int r, int s, int e) {
  if (r < s || e < l)
    return;
  pushdown(nd, l, r);
  if (s <= l && r <= e) {
    lazy[nd]++;
    pushdown(nd, l, r);
    return;
  }
  int m = (l + r) / 2;
  update(nd * 2, l, m, s, e);
  update(nd * 2 + 1, m + 1, r, s, e);
  if (tree[nd * 2] > tree[nd * 2 + 1]) {
    tree[nd] = tree[nd * 2 + 1];
    tree2[nd] = tree2[nd * 2 + 1];
  } else {
    tree[nd] = tree[nd * 2];
    tree2[nd] = tree2[nd * 2];
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    b[i] = a[i];
  }
  sort(b + 1, b + 1 + n);
  m = unique(b + 1, b + 1 + n) - b - 1;
  initseg(1, 1, m);
  for (int i = 1; i <= n; i++) {
    P tp = query(1, 1, m, lower_bound(b + 1, b + 1 + m, a[i]) - b, m);
    ans += (ll)tp.first - a[i];
    if (tp.second == 1)
      continue;
    update(1, 1, m, 1, tp.second - 1);
  }
  cout << ans;
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int N = 2e5 + 5;
int n, m, a[N], b[N], tree[4 * N], tree2[4 * N], lazy[4 * N];
ll ans;
void initseg(int nd, int l, int r) {
  if (l == r) {
    tree[nd] = b[l];
    tree2[nd] = l;
    return;
  }
  int m = (l + r) / 2;
  initseg(nd * 2, l, m);
  initseg(nd * 2 + 1, m + 1, r);
  tree[nd] = tree[nd * 2];
  tree2[nd] = l;
}
void pushdown(int nd, int l, int r) {
  if (l != r) {
    lazy[nd * 2] += lazy[nd];
    lazy[nd * 2 + 1] += lazy[nd];
  }
  tree[nd] += lazy[nd];
  lazy[nd] = 0;
}
P query(int nd, int l, int r, int s, int e) {
  if (r < s || e < l)
    return P(2e9, 2e9);
  pushdown(nd, l, r);
  if (s <= l && r <= e)
    return P(tree[nd], tree2[nd]);
  int m = (l + r) / 2;
  P p1 = query(nd * 2, l, m, s, e);
  P p2 = query(nd * 2 + 1, m + 1, r, s, e);
  if (p1.first > p2.first)
    return p2;
  return p1;
}
void update(int nd, int l, int r, int s, int e) {
  if (r < s || e < l)
    return;
  pushdown(nd, l, r);
  if (s <= l && r <= e) {
    lazy[nd]++;
    pushdown(nd, l, r);
    return;
  }
  int m = (l + r) / 2;
  update(nd * 2, l, m, s, e);
  update(nd * 2 + 1, m + 1, r, s, e);
  if (tree[nd * 2] > tree[nd * 2 + 1]) {
    tree[nd] = tree[nd * 2 + 1];
    tree2[nd] = tree2[nd * 2 + 1];
  } else {
    tree[nd] = tree[nd * 2];
    tree2[nd] = tree2[nd * 2];
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    b[i] = a[i];
  }
  sort(b + 1, b + 1 + n);
  m = unique(b + 1, b + 1 + n) - b - 1;
  initseg(1, 1, m);
  for (int i = 1; i <= n; i++) {
    int plc = lower_bound(b + 1, b + 1 + m, a[i]) - b;
    P tp = query(1, 1, m, plc, m);
    ans += (ll)tp.first - a[i];
    if (plc == 1)
      continue;
    update(1, 1, m, 1, plc - 1);
  }
  cout << ans;
  return 0;
}


#include <bits/stdc++.h>
#define xx first
#define yy second
#define mp make_pair
#define pb push_back
#define mset(x, y) memset(x, y, sizeof x)
#define mcpy(x, y) memcpy(x, y, sizeof x)
using namespace std;

typedef long long LL;
typedef pair<int, int> pii;

inline int Read() {
  int x = 0, f = 1, c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = getchar())
    x = x * 10 + c - '0';
  return x * f;
}

const int MAXN = 200005;

int n, m, a[MAXN], b[MAXN], tag[MAXN];
pii val[MAXN << 2];
LL ans;

inline void Build(int x, int l, int r) {
  val[x] = mp(b[l], l);
  if (l == r)
    return;
  int mid = l + r >> 1;
  Build(x << 1, l, mid), Build(x << 1 | 1, mid + 1, r);
}

inline void Modify(int x, int l, int r, int ql, int qr) {
  if (l == ql && r == qr)
    return (void)(val[x].xx++, tag[x]++);
  if (tag[x])
    val[x << 1].xx += tag[x], tag[x << 1] += tag[x],
        val[x << 1 | 1].xx += tag[x], tag[x << 1 | 1] += tag[x], tag[x] = 0;
  int mid = l + r >> 1;
  if (qr <= mid)
    Modify(x << 1, l, mid, ql, qr);
  else if (ql > mid)
    Modify(x << 1 | 1, mid + 1, r, ql, qr);
  else
    Modify(x << 1, l, mid, ql, mid),
        Modify(x << 1 | 1, mid + 1, r, mid + 1, qr);
  val[x] = min(val[x << 1], val[x << 1 | 1]);
}

inline pii Query(int x, int l, int r, int ql, int qr) {
  if (l == ql && r == qr)
    return val[x];
  if (tag[x])
    val[x << 1].xx += tag[x], tag[x << 1] += tag[x],
        val[x << 1 | 1].xx += tag[x], tag[x << 1 | 1] += tag[x], tag[x] = 0;
  int mid = l + r >> 1;
  if (qr <= mid)
    return Query(x << 1, l, mid, ql, qr);
  if (ql > mid)
    return Query(x << 1 | 1, mid + 1, r, ql, qr);
  return min(Query(x << 1, l, mid, ql, mid),
             Query(x << 1 | 1, mid + 1, r, mid + 1, qr));
}

int main() {
#ifdef wxh010910
  freopen("data.in", "r", stdin);
#endif
  n = Read();
  for (int i = 1; i <= n; i++)
    a[i] = b[i] = Read(), ans -= a[i];
  sort(b + 1, b + n + 1), m = unique(b + 1, b + n + 1) - b - 1, Build(1, 1, m);
  for (int i = 1; i <= n; i++) {
    pii val = Query(1, 1, m, lower_bound(b + 1, b + m + 1, a[i]) - b, m);
    ans += val.xx;
    if (val.yy > 1)
      Modify(1, 1, m, 1, val.yy - 1);
  }
  return printf("%lld\\n", ans), 0;
}

#include <bits/stdc++.h>
#define xx first
#define yy second
#define mp make_pair
#define pb push_back
#define mset(x, y) memset(x, y, sizeof x)
#define mcpy(x, y) memcpy(x, y, sizeof x)
using namespace std;

typedef long long LL;
typedef pair<int, int> pii;

inline int Read() {
  int x = 0, f = 1, c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = getchar())
    x = x * 10 + c - '0';
  return x * f;
}

const int MAXN = 200005;

int n, m, a[MAXN], b[MAXN], tag[MAXN << 2];
pii val[MAXN << 2];
LL ans;

inline void Build(int x, int l, int r) {
  val[x] = mp(b[l], l);
  if (l == r)
    return;
  int mid = l + r >> 1;
  Build(x << 1, l, mid), Build(x << 1 | 1, mid + 1, r);
}

inline void Modify(int x, int l, int r, int ql, int qr) {
  if (l == ql && r == qr)
    return (void)(val[x].xx++, tag[x]++);
  if (tag[x])
    val[x << 1].xx += tag[x], tag[x << 1] += tag[x],
        val[x << 1 | 1].xx += tag[x], tag[x << 1 | 1] += tag[x], tag[x] = 0;
  int mid = l + r >> 1;
  if (qr <= mid)
    Modify(x << 1, l, mid, ql, qr);
  else if (ql > mid)
    Modify(x << 1 | 1, mid + 1, r, ql, qr);
  else
    Modify(x << 1, l, mid, ql, mid),
        Modify(x << 1 | 1, mid + 1, r, mid + 1, qr);
  val[x] = min(val[x << 1], val[x << 1 | 1]);
}

inline pii Query(int x, int l, int r, int ql, int qr) {
  if (l == ql && r == qr)
    return val[x];
  if (tag[x])
    val[x << 1].xx += tag[x], tag[x << 1] += tag[x],
        val[x << 1 | 1].xx += tag[x], tag[x << 1 | 1] += tag[x], tag[x] = 0;
  int mid = l + r >> 1;
  if (qr <= mid)
    return Query(x << 1, l, mid, ql, qr);
  if (ql > mid)
    return Query(x << 1 | 1, mid + 1, r, ql, qr);
  return min(Query(x << 1, l, mid, ql, mid),
             Query(x << 1 | 1, mid + 1, r, mid + 1, qr));
}

int main() {
#ifdef wxh010910
  freopen("data.in", "r", stdin);
#endif
  n = Read();
  for (int i = 1; i <= n; i++)
    a[i] = b[i] = Read(), ans -= a[i];
  sort(b + 1, b + n + 1), m = unique(b + 1, b + n + 1) - b - 1, Build(1, 1, m);
  for (int i = 1; i <= n; i++) {
    pii val = Query(1, 1, m, lower_bound(b + 1, b + m + 1, a[i]) - b, m);
    ans += val.xx;
    if (val.yy > 1)
      Modify(1, 1, m, 1, val.yy - 1);
  }
  return printf("%lld\\n", ans), 0;
}


#include <bits/stdc++.h>
#define dbg() cerr <<
#define name(x) (#x) << ": " << (x) << ' ' <<

using namespace std;

struct SegmTree {
  int n;
  vector<int> a, tree, lazy, where;
  SegmTree(const vector<int> a_)
      : n(a_.size()), a(a_), tree(4 * n), lazy(4 * n), where(4 * n) {
    Build(0, 0, n - 1);
  }
  void Recalc(int node) {
    if (tree[2 * node + 1] < tree[2 * node + 2]) {
      tree[node] = tree[2 * node + 1];
      where[node] = where[2 * node + 1];
    } else {
      tree[node] = tree[2 * node + 2];
      where[node] = where[2 * node + 2];
    }
  }
  void Build(int node, int left, int right) {
    if (left == right) {
      tree[node] = a[left];
      where[node] = left;
      return;
    }
    int mid = left + (right - left) / 2;
    Build(2 * node + 1, left, mid);
    Build(2 * node + 2, mid + 1, right);
    Recalc(node);
  }
  inline void Add(int node, int val) {
    tree[node] += val;
    lazy[node] += val;
  }
  inline void Propagate(int node) {
    if (lazy[node] == 0)
      return;
    Add(2 * node + 1, lazy[node]);
    Add(2 * node + 2, lazy[node]);
    lazy[node] = 0;
  }
  void Update(int node, int left, int right, int pos) {
    if (pos >= right) {
      Add(node, 1);
      return;
    }
    Propagate(node);
    int mid = left + (right - left) / 2;
    Update(2 * node + 1, left, mid, pos);
    if (mid < pos)
      Update(2 * node + 2, mid + 1, right, pos);
    Recalc(node);
  }
  pair<int, int> Query(int node, int left, int right, int pos) {
    if (left >= pos) {
      return make_pair(tree[node], where[node]);
    }
    Propagate(node);
    int mid = left + (right - left) / 2;
    pair<int, int> ret = {(int)2e9, -1};
    if (pos <= mid)
      ret = Query(2 * node + 1, left, mid, pos);
    ret = min(ret, Query(2 * node + 2, mid + 1, right, pos));
    return ret;
  }
  pair<int, int> Query(int pos) { return Query(0, 0, n - 1, pos); }
  void Update(int pos) {
    if (pos >= 0)
      Update(0, 0, n - 1, pos);
  }
};

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int n;
  cin >> n;
  vector<int> a(n);
  for (int &x : a) {
    cin >> x;
  }
  auto b = a;
  sort(b.begin(), b.end());
  b.erase(unique(b.begin(), b.end()), b.end());
  SegmTree st(b);
  long long ans = 0;
  for (int i = 0; i < n; ++i) {
    int pos = find(b.begin(), b.end(), a[i]) - b.begin();
    pair<int, int> val = st.Query(pos);
    ans += val.first - a[i];
    st.Update(val.second - 1);
  }
  cout << ans << endl;

  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\\n";
}

#include <bits/stdc++.h>
#define dbg() cerr <<
#define name(x) (#x) << ": " << (x) << ' ' <<

using namespace std;

struct SegmTree {
  int n;
  vector<int> a, tree, lazy, where;
  SegmTree(const vector<int> a_)
      : n(a_.size()), a(a_), tree(4 * n), lazy(4 * n), where(4 * n) {
    Build(0, 0, n - 1);
  }
  void Recalc(int node) {
    if (tree[2 * node + 1] < tree[2 * node + 2]) {
      tree[node] = tree[2 * node + 1];
      where[node] = where[2 * node + 1];
    } else {
      tree[node] = tree[2 * node + 2];
      where[node] = where[2 * node + 2];
    }
  }
  void Build(int node, int left, int right) {
    if (left == right) {
      tree[node] = a[left];
      where[node] = left;
      return;
    }
    int mid = left + (right - left) / 2;
    Build(2 * node + 1, left, mid);
    Build(2 * node + 2, mid + 1, right);
    Recalc(node);
  }
  inline void Add(int node, int val) {
    tree[node] += val;
    lazy[node] += val;
  }
  inline void Propagate(int node) {
    if (lazy[node] == 0)
      return;
    Add(2 * node + 1, lazy[node]);
    Add(2 * node + 2, lazy[node]);
    lazy[node] = 0;
  }
  void Update(int node, int left, int right, int pos) {
    if (pos >= right) {
      Add(node, 1);
      return;
    }
    Propagate(node);
    int mid = left + (right - left) / 2;
    Update(2 * node + 1, left, mid, pos);
    if (mid < pos)
      Update(2 * node + 2, mid + 1, right, pos);
    Recalc(node);
  }
  pair<int, int> Query(int node, int left, int right, int pos) {
    if (left >= pos) {
      return make_pair(tree[node], where[node]);
    }
    Propagate(node);
    int mid = left + (right - left) / 2;
    pair<int, int> ret = {(int)2e9, -1};
    if (pos <= mid)
      ret = Query(2 * node + 1, left, mid, pos);
    ret = min(ret, Query(2 * node + 2, mid + 1, right, pos));
    return ret;
  }
  pair<int, int> Query(int pos) { return Query(0, 0, n - 1, pos); }
  void Update(int pos) {
    if (pos >= 0)
      Update(0, 0, n - 1, pos);
  }
};

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int n;
  cin >> n;
  vector<int> a(n);
  for (int &x : a) {
    cin >> x;
  }
  auto b = a;
  sort(b.begin(), b.end());
  b.erase(unique(b.begin(), b.end()), b.end());
  SegmTree st(b);
  long long ans = 0;
  for (int i = 0; i < n; ++i) {
    int pos = lower_bound(b.begin(), b.end(), a[i]) - b.begin();
    pair<int, int> val = st.Query(pos);
    ans += val.first - a[i];
    st.Update(val.second - 1);
  }
  cout << ans << endl;

  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\\n";
}


#include <algorithm>
#include <cstdio>
using namespace std;
int n, w[201000], Loc[201000], G[201000];
struct point {
  int x, num;
  bool operator<(const point &p) const {
    return x != p.x ? x < p.x : num < p.num;
  }
} ord[210000];
void RTE() {
  RTE();
  RTE();
}
int main() {
  int i, j;
  scanf("%d", &n);
  if (n > 10000) {
    RTE();
  }
  for (i = 1; i <= n; i++) {
    scanf("%d", &w[i]);
    Loc[i] = i;
    G[i] = i;
    ord[i] = {w[i], i};
  }
  sort(ord + 1, ord + n + 1);
  long long res = 0;
  for (i = 1; i <= n; i++) {
    int t = Loc[ord[i].num];
    int M = 0, Mn = 2e9, xx = 0, U = -1;
    for (j = 1; j <= t; j++) {
      if (Mn >= max(0, M - ord[i].x) + (t - j)) {
        Mn = max(0, M - ord[i].x) + (t - j);
        U = max(M, ord[i].x);
        xx = j;
      }
      M = max(M, w[G[j]]);
    }
    w[ord[i].num] = U;
    res += Mn;
    for (j = t; j > xx; j--) {
      swap(G[j - 1], G[j]);
      swap(Loc[G[j - 1]], Loc[G[j]]);
    }
  }
  printf("%lld\\n", res);
}

#include <algorithm>
#include <cstdio>
using namespace std;
#define INF ((1 << 30) - 1)
int n;
struct Tree {
  Tree *c1, *c2;
  int L, S, pv;
  Tree(int b, int e) {
    L = 0;
    c1 = c2 = NULL;
    pv = b - 1;
    S = -1 * (e - b + 1);
  }
  Tree GetMax(int b, int e, int s) {
    if (b == s)
      return *this;
    int m = (b + e) >> 1;
    if (!c1) {
      c1 = new Tree(b, m);
      c2 = new Tree(m + 1, e);
    }
    if (m < s)
      return c2->GetMax(m + 1, e, s);
    Tree ret = c1->GetMax(b, m, s);
    if (ret.L < ret.S + c2->L) {
      ret.L = ret.S + c2->L;
      ret.pv = c2->pv;
    }
    ret.S = ret.S + c2->S;
    return ret;
  }
  void Add(int b, int e, int x) {
    if (b == e) {
      S++;
      if (S > 0) {
        L = S;
        pv = b;
      }
      return;
    }
    int m = (b + e) >> 1;
    if (c1 == NULL) {
      c1 = new Tree(b, m);
      c2 = new Tree(m + 1, e);
    }
    if (x <= m)
      c1->Add(b, m, x);
    else
      c2->Add(m + 1, e, x);
    S = c1->S + c2->S;
    L = max(c1->L, c1->S + c2->L);
    pv = c1->pv;
    if (L != c1->L)
      pv = c2->pv;
  }
  int Sum(int b, int e, int x) {
    if (b == x)
      return S;
    int m = (b + e) >> 1, r = 0;
    if (!c1) {
      c1 = new Tree(b, m);
      c2 = new Tree(m + 1, e);
    }
    if (x <= m) {
      r = c2->S + c1->Sum(b, m, x);
    } else {
      r = c2->Sum(m + 1, e, x);
    }
    return r;
  }
};
int main() {
  int i, a;
  scanf("%d", &n);
  long long res = 0, SS = 0;
  Tree *Root = new Tree(0, INF);
  for (i = 1; i <= n; i++) {
    scanf("%d", &a);
    Tree rr = Root->GetMax(0, INF, a + 1);
    res += rr.pv - a;
    res += Root->Sum(0, INF, rr.pv + 1) + (INF - rr.pv);
    Root->Add(0, INF, rr.pv);
    SS += rr.pv - a;
  }
  printf("%lld\\n", res);
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define fi first
#define se second
#define mp make_pair
#define pb push_back

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef long double ld;
typedef tree<ii, null_type, less<ii>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

int a[222222];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  vector<int> vec;
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    if (vec.empty() || a[i] >= vec.back()) {
      vec.pb(a[i]);
      continue;
    }
    int mn = int(1e9);
    int aft = i - 1;
    int x = 0;
    for (int j = i - 1; j >= -1; j--) {
      int val = i - 1 - j + max(0, (j >= 0 ? vec[j] : -int(1e9)) - a[i]);
      if (val < mn) {
        mn = val;
        aft = j;
        x = max(0, (j >= 0 ? vec[j] : -int(1e9)) - a[i]);
      }
    }
    vi nw;
    ans += mn;
    for (int j = 0; j <= aft; j++)
      nw.pb(vec[j]);
    nw.pb(a[i] + x);
    for (int j = aft + 1; j < i; j++)
      nw.pb(vec[j]);
    vec = nw;
  }
  cout << ans << '\\n';
}

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define fi first
#define se second
#define mp make_pair
#define pb push_back

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef long double ld;
typedef tree<ii, null_type, less<ii>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

const int N = 455555;
const int INF = int(1e9);
int a[N];

/*
struct node
{
        int v,lazy;
};
node st[N*4];
void build(int id, int l, int r)
{
        st[id].lazy=0;st[id].v=INF;
        if(r-l>=2)
        {
                int mid=(l+r)>>1;
                build(id*2,l,mid); build(id*2+1,mid,r);
        }
}

void push(int id, int l, int r)
{
        if(st[id].lazy!=0)
        {
                st[id].v+=st[id].lazy;
                if(r-l>=2)
                {
                        st[id*2].lazy+=st[id].lazy;
st[id*2+1].lazy+=st[id].lazy;
                }
                st[id].lazy=0;
        }
}

void update(int id, int l, int r, int pos, int v)
{
        push(id,l,r);
        if(pos>=r||pos<l) return ;
        if(r-l<2)
        {
                st[id].v=v; return ;
        }
        int mid=(l+r)>>1;
        update(id*2,l,mid,pos,v); update(id*2+1,mid,r,pos,v);
        st[id].v=min(st[id*2].v,st[id*2+1].v);
}

void updaterange(int id, int l, int r, int ql, int qr, int v)
{
        push(id,l,r);
        if(ql>=r||l>=qr) return ;
        if(ql<=l&&r<=qr)
        {
                st[id].lazy+=v; push(id,l,r); return ;
        }
        int mid=(l+r)>>1;
        updaterange(id*2,l,mid,ql,qr,v); updaterange(id*2+1,mid,r,ql,qr,v);
        st[id].v=min(st[id*2].v,st[id*2+1].v);
}

ii query(int id, int l, int r, int ql, int qr)
{
        push(id,l,r);
        if(ql>=r||l>=qr) return mp(INF, -1);
        if(ql<=l&&r<=qr) return mp(st[id].v, l);
        int mid=(l+r)>>1;
        return min(query(id*2,l,mid,ql,qr),query(id*2+1,mid,r,ql,qr));
}
*/

struct node {
  int prior, siz;
  ii val;   // value stored in the array
  ii sum;   // whatever info you want to maintain in segtree for each node
  int lazy; // whatever lazy update you want to do
  struct node *l, *r;
};

node buffer[555555];
typedef node *pnode;

int sz(pnode t) { return t ? t->siz : 0; }
void upd_sz(pnode t) {
  if (t)
    t->siz = sz(t->l) + 1 + sz(t->r);
}
void lazy(pnode t) {
  if (!t || !t->lazy)
    return;
  t->val.fi += t->lazy; // operation of lazy
  t->sum.fi += t->lazy;
  if (t->l)
    t->l->lazy += t->lazy; // propagate lazy
  if (t->r)
    t->r->lazy += t->lazy;
  t->lazy = 0;
}
void reset(pnode t) {
  if (t)
    t->sum = t->val; // no need to reset lazy coz when we call this lazy would
                     // itself be propagated
}
void combine(pnode &t, pnode l, pnode r) { // combining two ranges of segtree
  if (!l || !r)
    return void(t = l ? l : r);
  t->sum = min(l->sum, r->sum);
}
void operation(pnode t) { // operation of segtree
  if (!t)
    return;
  reset(t); // reset the value of current node assuming it now represents a
            // single element of the array
  lazy(t->l);
  lazy(t->r); // imp:propagate lazy before combining t->l,t->r;
  combine(t, t->l, t);
  combine(t, t, t->r);
}
void split(pnode t, pnode &l, pnode &r, int pos, int add = 0) {
  // if(pos<0&&add==0){l=NULL; r=t; return ;}
  if (!t)
    return void(l = r = NULL);
  lazy(t);
  int curr_pos = add + sz(t->l);
  if (curr_pos <= pos) // element at pos goes to left subtree(l)
    split(t->r, t->r, r, pos, curr_pos + 1), l = t;
  else
    split(t->l, l, t->l, pos, add), r = t;
  upd_sz(t);
  operation(t);
}
void merge(pnode &t, pnode l,
           pnode r) { // l->leftarray,r->rightarray,t->resulting array
  lazy(l);
  lazy(r);
  if (!l || !r)
    t = l ? l : r;
  else if (l->prior > r->prior)
    merge(l->r, l->r, r), t = l;
  else
    merge(r->l, l, r->l), t = r;
  upd_sz(t);
  operation(t);
}

pnode curnode = &buffer[0];
pnode init(ii val) {
  pnode ret = curnode++;
  ret->prior = rand();
  ret->siz = 1;
  ret->val = val;
  ret->sum = val;
  ret->lazy = 0;
  return ret;
}
ii range_query(pnode t, int l, int r) { //[l,r]
  if (l > r)
    return mp(INF, -1);
  pnode L, mid, R;
  split(t, L, mid, l - 1);
  split(mid, t, R, r - l); // note: r-l!!
  ii ans = t->sum;
  merge(mid, L, t);
  merge(t, mid, R);
  return ans;
}
void range_update(pnode t, int l, int r, ll val) { //[l,r]
  if (l > r)
    return;
  pnode L, mid, R;
  split(t, L, mid, l - 1);
  split(mid, t, R, r - l); // note: r-l!!
  t->lazy += val;          // lazy_update
  merge(mid, L, t);
  merge(t, mid, R);
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  pbds T;
  int timer = 0;
  ll ans = 0;
  pnode treap = NULL;
  for (int i = 0; i < n; i++) {
    int aft = T.order_of_key(mp(a[i] + 1, -1)) - 1;
    int mn = i - 1 - aft;
    if (mn == 0) {
      T.insert(mp(a[i], ++timer));
      pnode tmp = init(mp(a[i] - i, a[i]));
      merge(treap, treap, tmp);
      continue;
    }
    int id = aft + 1;
    aft = -1;
    ii X = range_query(treap, id + 1, i - 1);
    if (X.fi + i - 1 - a[i] < mn) {
      mn = X.fi + i - 1 - a[i];
      aft = X.se;
    }
    ans += mn;
    // cerr<<"MN : "<<mn<<" AFT : "<<aft<<endl;
    if (aft >= 0) {
      int pos = T.order_of_key(mp(aft + 1, -1));
      range_update(treap, pos, i - 1, -1);
      T.insert(mp(aft, ++timer));
      pnode tmp = init(mp(aft - pos, aft));
      pnode L, R;
      split(treap, L, R, pos - 1);
      merge(L, L, tmp);
      merge(treap, L, R);
    } else {
      aft = a[i];
      int pos = T.order_of_key(mp(aft + 1, -1));
      range_update(treap, pos, i - 1, -1);
      T.insert(mp(aft, ++timer));
      pnode tmp = init(mp(aft - pos, aft));
      pnode L, R;
      split(treap, L, R, pos - 1);
      merge(L, L, tmp);
      merge(treap, L, R);
    }
    // cerr<<"MN "<<mn<<' '<<"AFT "<<aft<<'\\n';
  }
  cout << ans << '\\n';
}


#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef long double llf;
typedef pair<lint, lint> pi;
const int MAXN = 2005, N = MAXN;
const int mod = 1e9 + 7, inv2 = (mod + 1) / 2;
int dp[MAXN][3][MAXN], odp[3][MAXN], sz[MAXN];
vector<int> adj[N];
void sadd(int &x, int y) {
  x += y;
  while (x >= mod)
    x -= mod;
}
void dfs(int v, int p = -1) {
  dp[v][0][1] = 1;
  sz[v] = 1;
  for (int u : adj[v]) {
    if (u != p) {
      dfs(u, v);
      memcpy(odp, dp[v], sizeof dp[v]);
      memset(dp[v], 0, sizeof dp[v]);
      for (int dv = 0; dv <= 2; ++dv) {
        for (int du = 0; du <= 2; ++du) {
          for (int use = 0; use <= 1; ++use) {
            if (use == 1 && (du > 1 || dv > 1))
              continue;
            for (int cv = 1; cv <= sz[v]; ++cv)
              for (int cu = 1; cu <= sz[u]; ++cu) {
                int ways = (use == 1 ? mod - 1 : 1);
                if (use) {
                  if (du == 0 && dv == 0)
                    ways = (long long)ways * 2 % mod;
                  if (du == 1 && dv == 1)
                    ways = (long long)ways * inv2 % mod;
                }
                sadd(dp[v][dv + use][cv + cu - use],
                     odp[dv][cv] * (long long)dp[u][du][cu] % mod * ways % mod);
              }
          }
        }
      }
      sz[v] += sz[u];
    }
  }
}
int f[N], a[N];
int main() {
  int n;
  long long res = 0;
  cin >> n;
  for (int j = 0; j < n; ++j)
    cin >> a[j];
  for (int j = 0; j < n; ++j) {
    int best = 2e9;
    for (int i = j; i >= 0; --i) {
      if (max(i == 0 ? 0 : a[i - 1] - a[j], 0) + j - i < best) {
        best = max(i == 0 ? 0 : a[i - 1] - a[j], 0) + j - i;
      }
    }
    for (int i = j; i >= 0; --i) {
      if (max(i == 0 ? 0 : a[i - 1] - a[j], 0) + j - i == best) {
        for (int k = j; k > i; --k) {
          swap(a[k], a[k - 1]);
        }
        a[i] = max(a[i], (i == 0 ? 0 : a[i - 1]));
      }
    }
    res += best;
  }
  cout << res << endl;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef pair<int, int> pi;
const int MAXN = 200005;
const int MAXT = 530000;

int n, a[MAXN];
vector<int> v;

struct seg {
  pi tree[MAXT];
  int lazy[MAXT];
  void init(int s, int e, int p, vector<int> &v) {
    if (s == e) {
      tree[p] = pi(v[s], s);
      return;
    }
    int m = (s + e) / 2;
    init(s, m, 2 * p, v);
    init(m + 1, e, 2 * p + 1, v);
    tree[p] = min(tree[2 * p], tree[2 * p + 1]);
  }
  void lazydown(int p) {
    tree[2 * p].first += lazy[p];
    tree[2 * p + 1].first += lazy[p];
    lazy[2 * p] += lazy[p];
    lazy[2 * p + 1] += lazy[p];
    lazy[p] = 0;
  }
  void add(int s, int e, int ps, int pe, int p, int v) {
    if (e < ps || pe < s)
      return;
    if (s <= ps && pe <= e) {
      tree[p].first += v;
      lazy[p] += v;
      return;
    }
    int pm = (ps + pe) / 2;
    lazydown(p);
    add(s, e, ps, pm, 2 * p, v);
    add(s, e, pm + 1, pe, 2 * p + 1, v);
    tree[p] = min(tree[2 * p], tree[2 * p + 1]);
  }
  pi query(int s, int e, int ps, int pe, int p) {
    if (e < ps || pe < s)
      return pi(2e9, -1);
    if (s <= ps && pe <= e)
      return tree[p];
    int pm = (ps + pe) / 2;
    lazydown(p);
    return min(query(s, e, ps, pm, 2 * p), query(s, e, pm + 1, pe, 2 * p + 1));
  }
} seg;

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    v.push_back(a[i]);
  }
  sort(v.begin(), v.end());
  v.resize(unique(v.begin(), v.end()) - v.begin());
  seg.init(0, v.size() - 1, 1, v);
  lint ans = 0;
  for (int i = 0; i < n; i++) {
    a[i] = lower_bound(v.begin(), v.end(), a[i]) - v.begin();
    auto ret = seg.query(a[i], v.size() - 1, 0, v.size() - 1, 1);
    ans += ret.first - v[a[i]];
    seg.add(0, ret.second - 1, 0, v.size() - 1, 1, 1);
  }
  cout << ans << endl;
}


/**
 *    author:  tourist
 *    created: 27.11.2019 18:36:41
 **/
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    int best_j = -1;
    int best_c = 787788;
    for (int j = 0; j <= i; j++) {
      int cur_c = j + (j == i ? 0 : max(0, a[i - 1 - j] - a[i]));
      if (cur_c < best_c) {
        best_c = cur_c;
        best_j = j;
      }
    }
    ans += best_c;
    if (best_j < i) {
      a[i] = max(a[i], a[i - 1 - best_j]);
    }
    for (int j = 0; j < best_j; j++) {
      swap(a[i - j], a[i - j - 1]);
    }
  }
  cout << ans << '\\n';
  return 0;
}

/**
 *    author:  tourist
 *    created: 27.11.2019 18:36:41
 **/
#include <bits/stdc++.h>

using namespace std;

class segtree {
public:
  struct node {
    // don't forget to set default value (used for leaves)
    // not necessarily neutral element!
    int mn = 0;
    int add = 0;
    int id = -1;

    void apply(int l, int r, int v) {
      mn += v;
      add += v;
      if (l == r) {
        id = l;
      }
    }
  };

  node unite(const node &a, const node &b) const {
    node res;
    res.mn = min(a.mn, b.mn);
    res.id = (res.mn == a.mn ? a.id : b.id);
    return res;
  }

  inline void push(int x, int l, int r) {
    int y = (l + r) >> 1;
    int z = x + ((y - l + 1) << 1);
    if (tree[x].add != 0) {
      tree[x + 1].apply(l, y, tree[x].add);
      tree[z].apply(y + 1, r, tree[x].add);
      tree[x].add = 0;
    }
  }

  inline void pull(int x, int z) { tree[x] = unite(tree[x + 1], tree[z]); }

  int n;
  vector<node> tree;

  void build(int x, int l, int r) {
    if (l == r) {
      return;
    }
    int y = (l + r) >> 1;
    int z = x + ((y - l + 1) << 1);
    build(x + 1, l, y);
    build(z, y + 1, r);
    pull(x, z);
  }

  template <typename M> void build(int x, int l, int r, const vector<M> &v) {
    if (l == r) {
      tree[x].apply(l, r, v[l]);
      return;
    }
    int y = (l + r) >> 1;
    int z = x + ((y - l + 1) << 1);
    build(x + 1, l, y, v);
    build(z, y + 1, r, v);
    pull(x, z);
  }

  node get(int x, int l, int r, int ll, int rr) {
    if (ll <= l && r <= rr) {
      return tree[x];
    }
    int y = (l + r) >> 1;
    int z = x + ((y - l + 1) << 1);
    push(x, l, r);
    node res{};
    if (rr <= y) {
      res = get(x + 1, l, y, ll, rr);
    } else {
      if (ll > y) {
        res = get(z, y + 1, r, ll, rr);
      } else {
        res = unite(get(x + 1, l, y, ll, rr), get(z, y + 1, r, ll, rr));
      }
    }
    pull(x, z);
    return res;
  }

  template <typename... M>
  void modify(int x, int l, int r, int ll, int rr, const M &...v) {
    if (ll <= l && r <= rr) {
      tree[x].apply(l, r, v...);
      return;
    }
    int y = (l + r) >> 1;
    int z = x + ((y - l + 1) << 1);
    push(x, l, r);
    if (ll <= y) {
      modify(x + 1, l, y, ll, rr, v...);
    }
    if (rr > y) {
      modify(z, y + 1, r, ll, rr, v...);
    }
    pull(x, z);
  }

  int find_first_knowingly(int x, int l, int r,
                           const function<bool(const node &)> &f) {
    if (l == r) {
      return l;
    }
    push(x, l, r);
    int y = (l + r) >> 1;
    int z = x + ((y - l + 1) << 1);
    int res;
    if (f(tree[x + 1])) {
      res = find_first_knowingly(x + 1, l, y, f);
    } else {
      res = find_first_knowingly(z, y + 1, r, f);
    }
    pull(x, z);
    return res;
  }

  int find_first(int x, int l, int r, int ll, int rr,
                 const function<bool(const node &)> &f) {
    if (ll <= l && r <= rr) {
      if (!f(tree[x])) {
        return -1;
      }
      return find_first_knowingly(x, l, r, f);
    }
    push(x, l, r);
    int y = (l + r) >> 1;
    int z = x + ((y - l + 1) << 1);
    int res = -1;
    if (ll <= y) {
      res = find_first(x + 1, l, y, ll, rr, f);
    }
    if (rr > y && res == -1) {
      res = find_first(z, y + 1, r, ll, rr, f);
    }
    pull(x, z);
    return res;
  }

  int find_last_knowingly(int x, int l, int r,
                          const function<bool(const node &)> &f) {
    if (l == r) {
      return l;
    }
    push(x, l, r);
    int y = (l + r) >> 1;
    int z = x + ((y - l + 1) << 1);
    int res;
    if (f(tree[z])) {
      res = find_last_knowingly(z, y + 1, r, f);
    } else {
      res = find_last_knowingly(x + 1, l, y, f);
    }
    pull(x, z);
    return res;
  }

  int find_last(int x, int l, int r, int ll, int rr,
                const function<bool(const node &)> &f) {
    if (ll <= l && r <= rr) {
      if (!f(tree[x])) {
        return -1;
      }
      return find_last_knowingly(x, l, r, f);
    }
    push(x, l, r);
    int y = (l + r) >> 1;
    int z = x + ((y - l + 1) << 1);
    int res = -1;
    if (rr > y) {
      res = find_last(z, y + 1, r, ll, rr, f);
    }
    if (ll <= y && res == -1) {
      res = find_last(x + 1, l, y, ll, rr, f);
    }
    pull(x, z);
    return res;
  }

  segtree(int _n) : n(_n) {
    assert(n > 0);
    tree.resize(2 * n - 1);
    build(0, 0, n - 1);
  }

  template <typename M> segtree(const vector<M> &v) {
    n = v.size();
    assert(n > 0);
    tree.resize(2 * n - 1);
    build(0, 0, n - 1, v);
  }

  node get(int ll, int rr) {
    assert(0 <= ll && ll <= rr && rr <= n - 1);
    return get(0, 0, n - 1, ll, rr);
  }

  node get(int p) {
    assert(0 <= p && p <= n - 1);
    return get(0, 0, n - 1, p, p);
  }

  template <typename... M> void modify(int ll, int rr, const M &...v) {
    assert(0 <= ll && ll <= rr && rr <= n - 1);
    modify(0, 0, n - 1, ll, rr, v...);
  }

  // find_first and find_last call all FALSE elements
  // to the left (right) of the sought position exactly once

  int find_first(int ll, int rr, const function<bool(const node &)> &f) {
    assert(0 <= ll && ll <= rr && rr <= n - 1);
    return find_first(0, 0, n - 1, ll, rr, f);
  }

  int find_last(int ll, int rr, const function<bool(const node &)> &f) {
    assert(0 <= ll && ll <= rr && rr <= n - 1);
    return find_last(0, 0, n - 1, ll, rr, f);
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  long long ans = 0;
  vector<int> as = a;
  sort(as.begin(), as.end());
  segtree st(as);
  for (int i = 0; i < n; i++) {
    int pos = (int)(lower_bound(as.begin(), as.end(), a[i]) - as.begin());
    auto ret = st.get(pos, n - 1);
    ans += ret.mn - a[i];
    if (ret.id > 0) {
      st.modify(0, ret.id - 1, 1);
    }
  }
  cout << ans << '\\n';
  return 0;
}


#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <set>
#define ll long long
#define inf 2147483647
#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define repp(i, a, b, c) for (int i = a; i <= b; i += c)
#define reph(i, a, b) for (i = a; i <= b; ++i)
#define reg(i, head, d, a) for (int i = head[d]; i; i = a[i].next)
#define rap(i, a, b) for (int i = a; i >= b; --i)
#define me(a, b) memset(a, b, sizeof(a));
#define poi(lx, ly, rx, ry)                                                    \\
  ((lx > rx or ly > ry) ? 0                                                    \\
                        : (ice[rx][ry] + ice[lx - 1][ly - 1] -                 \\
                           ice[rx][ly - 1] - ice[lx - 1][ry]))
int n, m, sx, sy;
int ice[45][45], ans = inf, f[45][45][45][45];
char temp;

using namespace std;

int main(void) {

  freopen("haner.in", "r", stdin);
  freopen("haner.out", "w", stdout);

  ios::sync_with_stdio(0);

  cin >> n >> m;
  me(f, 0x7f);
  f[1][1][n][m] = 0;
  rep(i, 1, n) {
    rep(j, 1, m) {
      cin >> temp;
      if (temp == 'H')
        sx = i, sy = j;
      ice[i][j] = ice[i - 1][j] + ice[i][j - 1] - ice[i - 1][j - 1] +
                  ((temp == '#') ? 1 : 0);
    }
  }
  rep(i, 1, sx) {
    rep(j, 1, sy) {
      rap(o, n, sx) {
        rap(p, m, sy) {
          ans = min(ans, f[i][j][o][p] +
                             min(min(poi(i, j, sx, sy), poi(i, sy, sx, p)),
                                 min(poi(sx, j, o, sy), poi(sx, sy, o, p))));
          rep(s, i, o) {
            rep(t, j, p) {
              if (sx <= s and sy <= t)
                f[i][j][s][t] =
                    min(f[i][j][s][t], f[i][j][o][p] + poi(i, t + 1, s, p) +
                                           poi(s + 1, j, o, t));
              if (sx <= s and sy >= t)
                f[i][t][s][p] =
                    min(f[i][t][s][p], f[i][j][o][p] + poi(i, j, s, t - 1) +
                                           poi(s + 1, t, o, p));
              if (sx >= s and sy >= t)
                f[s][t][o][p] =
                    min(f[s][t][o][p], f[i][j][o][p] + poi(i, t, s - 1, p) +
                                           poi(s, j, o, t - 1));
              if (sx >= s and sy <= t)
                f[s][j][o][t] =
                    min(f[s][j][o][t], f[i][j][o][p] + poi(i, j, s - 1, t) +
                                           poi(s, t + 1, o, p));
            }
          }
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 45;
const int INF = 1e9;

int ice[MAXN][MAXN], dp[MAXN][MAXN][MAXN][MAXN];
int n, m, hx, hy, ans = INF;

inline int poi(int lx, int ly, int rx, int ry) {
  if (lx > rx || ly > ry)
    return 0;
  return ice[rx][ry] - ice[rx][ly - 1] - ice[lx - 1][ry] + ice[lx - 1][ly - 1];
}

int main() {
  ios::sync_with_stdio(0);
  cin >> n >> m;
  memset(dp, 0x3f, sizeof dp);
  dp[1][1][n][m] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      char c;
      cin >> c;
      if (c == 'P')
        hx = i, hy = j;
      ice[i][j] =
          ice[i - 1][j] + ice[i][j - 1] - ice[i - 1][j - 1] + (c == '#');
    }
  }
  for (int i = 1; i <= hx; i++)
    for (int j = 1; j <= hy; j++)
      for (int k = n; k >= hx; k--)
        for (int l = m; l >= hy; l--) {
          ans = min(ans, dp[i][j][k][l] +
                             min(min(poi(i, j, hx, hy), poi(i, hy, hx, l)),
                                 min(poi(hx, j, k, hy), poi(hx, hy, k, l))));
          for (int s = i; s <= k; s++)
            for (int t = j; t <= l; t++) {
              if (s >= hx && t >= hy)
                dp[i][j][s][t] =
                    min(dp[i][j][s][t], dp[i][j][k][l] + poi(s + 1, j, k, t) +
                                            poi(i, t + 1, s, l));
              if (s >= hx && t <= hy)
                dp[i][t][s][l] =
                    min(dp[i][t][s][l], dp[i][j][k][l] + poi(i, j, s, t - 1) +
                                            poi(s + 1, t, k, l));
              if (s <= hx && t <= hy)
                dp[s][t][k][l] =
                    min(dp[s][t][k][l], dp[i][j][k][l] + poi(s, j, k, t - 1) +
                                            poi(i, t, s - 1, l));
              if (s <= hx && t >= hy)
                dp[s][j][k][t] =
                    min(dp[s][j][k][t], dp[i][j][k][l] + poi(i, j, s - 1, t) +
                                            poi(s, t + 1, k, l));
            }
        }
  cout << ans << endl;
  return 0;
}