Directed Roads (Codeforces 711D)
Tags: Math, combinatorics

ZS the Coder and Chris the Baboon has explored Udayland for quite some time. They realize that it consists of n towns numbered from 1 to n. There are n directed roads in the Udayland. i-th of them goes from town i to some other town ai (ai ≠ i). ZS the Coder can flip the direction of any road in Udayland, i.e. if it goes from town A to town B before the flip, it will go from town B to town A after.ZS the Coder considers the roads in the Udayland confusing, if there is a sequence of distinct towns A1, A2, ..., Ak (k > 1) such that for every 1 ≤ i < k there is a road from town Ai to town Ai + 1 and another road from town Ak to town A1. In other words, the roads are confusing if some of them form a directed cycle of some towns.Now ZS the Coder wonders how many sets of roads (there are 2n variants) in initial configuration can he choose to flip such that after flipping each road in the set exactly once, the resulting network will not be confusing.Note that it is allowed that after the flipping there are more than one directed road from some town and possibly some towns with no roads leading out of it, or multiple roads between any pair of cities.

Input: The first line of the input contains single integer n (2 ≤ n ≤ 2·105) — the number of towns in Udayland.The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ n, ai ≠ i), ai denotes a road going from town i to town ai.

Output: Print a single integer — the number of ways to flip some set of the roads so that the resulting whole set of all roads is not confusing. Since this number may be too large, print the answer modulo 109 + 7.

Note: Consider the first sample case. There are 3 towns and 3 roads. The towns are numbered from 1 to 3 and the roads are , ,  initially. Number the roads 1 to 3 in this order. The sets of roads that ZS the Coder can flip (to make them not confusing) are {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}. Note that the empty set is invalid because if no roads are flipped, then towns 1, 2, 3 is form a directed cycle, so it is confusing. Similarly, flipping all roads is confusing too. Thus, there are a total of 6 possible sets ZS the Coder can flip.The sample image shows all possible ways of orienting the roads from the first sample such that the network is not confusing.

Sample Inputs:
3
2 3 1

4
2 1 1 1

5
2 4 2 5 3

Sample Outputs:
6

8

28

Solution (Python):
mod = 1000000007

def solve(A):
    n = len(A)
    aa = [0]
    for x in A:
        aa.append(x)
    idx = [0] * (n + 1)
    res = pos = 1
    for i in range(1, n + 1):
        if not idx[i]:
            (j, start) = (i, pos)
            while not idx[j]:
                idx[j] = pos
                pos += 1
                j = aa[j]
            if idx[j] >= start:
                n -= pos - idx[j]
                res = res * (pow(2, pos - idx[j], mod) - 2) % mod
    return res * pow(2, n, mod) % mod
nn = int(input())
aa = list(map(int, input().strip().split()))
print(solve(aa))

Solution (Python):
mod = 1000000007

def solve(A):
    n = len(A)
    aa = [0]
    for x in A:
        aa.append(x)
    idx = [0] * (n + 1)
    res = pos = 1
    for i in range(1, n + 1):
        if not idx[i]:
            (j, start) = (i, pos)
            while not idx[j]:
                idx[j] = pos
                pos += 1
                j = aa[j]
            if idx[j] >= start:
                n -= pos - idx[j]
                res = res * (pow(2, pos - idx[j], mod) - 2) % mod
    print(res * pow(2, n, mod) % mod)
nn = int(input())
aa = list(map(int, input().strip().split()))
solve(aa)

Solution (Python):
n = int(input())
l = list(map(int, input().split()))
l.insert(0, 0)
mark = {}
(loop, pos, res, mod) = (0, 1, 1, int(1000000000.0 + 7))
for i in range(1, n + 1):
    if not i in mark:
        (start, j) = (pos, i)
        while not j in mark:
            mark[j] = pos
            pos += 1
            j = l[j]
        if mark[j] >= start:
            size = pos - mark[j]
            loop += size
            res *= pow(2, size, mod) - 2 + mod
            res %= mod
res = res * pow(2, n - loop, mod) % mod
print(res)