import java.io.*;
import java.util.*;
import java.math.*;
import static java.
lang.
Math.
*; import static java.
util.
Arrays.
*;
class D {
static final char PROB = D.class.getSimpleName().charAt(0);
static final boolean PRAC = false;
static final int INF = 1 << 20;
static final int[] di = { -1, 0, 0, 1 };
static final int[] dj = { 0, -1, 1, 0 };
static Scanner sc
= new Scanner
(System.
in);
static final int MAX = 1000;
static final double[] g = new double[MAX + 1];
static final double[][] cef = new double[MAX + 1][MAX + 1];
final int N;
final int[] a;
public D() {
N = sc.nextInt();
a = new int[N];
for(int i =0 ; i < N; i++)
a[i] = sc.nextInt() - 1;
}
UnionFind uf = new UnionFind(N);
for(int i = 0; i < N; i++)
uf.union(i, a[i]);
double ans = 0;
for(int i : uf.t)
if(i < 0)
ans += f(-i);
return "" + ans;
}
static double f(int i) {
return i == 1 ? 0. : g[i] + 1.;
}
static double g(int i) {
double ret = 1.;
for(int j = 1; j < i; j++)
ret += cef[i][j] * (g[j] + f(i - j));
ret /= 1. - cef[i][0];
ret -= 1.;
return ret;
}
static double cef(int n, int k) {
double ret = 1.;
for(int i = 1; i <= k; i++) {
ret *= n - i;
ret /= i;
}
for(int i = 1; i < n; i++)
ret /= 2.;
return ret;
}
static {
for(int n = 1; n <= MAX; n++)
for(int k = 0; k < n; k++)
cef[n][k] = cef(n, k);
for(int i = 2; i <= MAX; i++)
g[i] = g(i);
}
class UnionFind {
final int[] t;
public UnionFind(int size) {
t = new int[size];
fill(t, -1);
}
void union(int x, int y) {
x = root(x);
y = root(y);
if(x == y)
return;
t[x] += t[y];
t[y] = x;
}
int root(int x) {
return t[x] < 0 ? x : (t[x] = root(t[x]));
}
}
public static void main
(String...
args) { // large();
int T
= Integer.
parseInt(sc.
nextLine()); for (int t = 1; t <= T; t++) {
System.
err.
printf("Case #%s%n", t
); System.
out.
printf("Case #%s: %s%n", t,
new D
().
solve()); }
}
public static void small() {
String in
= PROB
+ "-small" + (PRAC
? "-practice" : "-attempt" + 0) + ".in"; String out
= PROB
+ "-small.out"; try {
if (!PRAC)
for (int i
= 1; new File(PROB
+ "-small" + "-attempt" + i
+ ".in").
exists(); i
++) in = PROB + "-small" + "-attempt" + i + ".in";
e.printStackTrace();
}
}
public static void large() {
String in
= PROB
+ "-large" + (PRAC
? "-practice" : "") + ".in"; String out
= PROB
+ "-large.out"; try {
e.printStackTrace();
}
}
private static void debug
(Object...
os) { System.
err.
println(deepToString
(os
)); }
}
aW1wb3J0IGphdmEuaW8uKjsKaW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5tYXRoLio7CgppbXBvcnQgc3RhdGljIGphdmEubGFuZy5NYXRoLio7CmltcG9ydCBzdGF0aWMgamF2YS5sYW5nLkNoYXJhY3Rlci4qOwppbXBvcnQgc3RhdGljIGphdmEudXRpbC5BcnJheXMuKjsKaW1wb3J0IHN0YXRpYyBqYXZhLnV0aWwuQ29sbGVjdGlvbnMuKjsKaW1wb3J0IHN0YXRpYyBqYXZhLm1hdGguQmlnSW50ZWdlci4qOwoKY2xhc3MgRCB7CglzdGF0aWMgZmluYWwgY2hhciBQUk9CID0gRC5jbGFzcy5nZXRTaW1wbGVOYW1lKCkuY2hhckF0KDApOwoJc3RhdGljIGZpbmFsIGJvb2xlYW4gUFJBQyA9IGZhbHNlOwoKCXN0YXRpYyBmaW5hbCBpbnQgSU5GID0gMSA8PCAyMDsKCXN0YXRpYyBmaW5hbCBpbnRbXSBkaSA9IHsgLTEsIDAsIDAsIDEgfTsKCXN0YXRpYyBmaW5hbCBpbnRbXSBkaiA9IHsgMCwgLTEsIDEsIDAgfTsKCXN0YXRpYyBTY2FubmVyIHNjID0gbmV3IFNjYW5uZXIoU3lzdGVtLmluKTsKCQoJc3RhdGljIGZpbmFsIGludCBNQVggPSAxMDAwOwoJc3RhdGljIGZpbmFsIGRvdWJsZVtdIGcgPSBuZXcgZG91YmxlW01BWCArIDFdOwoJc3RhdGljIGZpbmFsIGRvdWJsZVtdW10gY2VmID0gbmV3IGRvdWJsZVtNQVggKyAxXVtNQV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