Under construction

Still being built. Things may change

Random Sort 2

Time limit2sMemory limit512 MB

Summary
Compute the expected number of random swaps the process needs to turn a given permutation of size up to 10 into increasing order.
Level

Medium7 of 10

Topics
Probability, Dynamic programming, Math, Matrix
Solved
No attempts yet

Problem

Random sort is a way to sort a permutation PP of size NN with the algorithm below.

function random_sort(permutation P) {
	swaps = 0;
	while (not sorted P) {
		(i, j) = random pair (1 <= i < j <= N)
		swap(P[i], P[j])
		swaps = swaps + 1;
	}
	return swaps;
}

random pair picks one of the (N2)\binom{N}{2} pairs that satisfy 1≤i<j≤N1 \le i < j \le N, uniformly at random and independently on every iteration. The loop stops once PP is in increasing order, and the function returns how many swaps it counted.

Given a permutation PP, write a program that computes the expected return value of random_sort.

Input

The first line contains the size of the permutation NN (2≤N≤102 \le N \le 10).

The second line contains the permutation P1,P2,…,PNP_1, P_2, \dots, P_N, separated by spaces. Every integer from 11 to NN appears exactly once.

Output

Print the expected return value of random_sort with exactly seven digits after the decimal point. Round half up at the eighth digit and pad with zeros when digits are left over. If the given permutation is already in increasing order, print 0.0000000.

Examples4

  1. Example 1

    Input
    2
    2 1
    
    Expected output
    1.0000000
    
  2. Example 2

    Input
    3
    2 1 3
    
    Expected output
    5.0000000
    
  3. Example 3

    Input
    4
    2 3 4 1
    
    Expected output
    27.5000000
    
  4. Example 4

    Input
    6
    2 5 1 3 4 6
    
    Expected output
    781.0416667