Codeforces Round #369 (Div. 2) D. Directed Roads Dfs to find the number of points on the ring for a unicom block

Source: Internet
Author: User

D. Directed Roads

ZS the coder and Chris the baboon have explored udayland for quite some time. They realize that it consists of n towns numbered from 1to n.

There is N directed roads in the Udayland. i-th of them goes from the town I to some the other town ai (a ii). 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'll go from town B to town A after.

ZS The coder considers the roads in the Udaylandconfusing, if there is a sequence of distinct Towns  a< /em>1, a 2, ..., A k   ( K > 1) such that for Every 1≤ I < k  there is a road from Town  a i  to town  A i + 1 and Another road from Town  A K  to Town   A 1. In other words, the roads is confusing If some of Them form a directed cycle of Some towns.

Now ZS the coder wonders how many sets of roads (there is 2n variants) in initial configuration can he C Hoose to flip such this after flipping all road in the set exactly once, the resulting network won't be confusing.

Note that it was allowed that after the flipping there be more than one directed road from some town and possibly some tow NS with no roads leading out of the IT, or multiple roads between any pair of cities.

Input

The first line of the input contains a single integer n (2≤ n ≤2 105)-the number of towns In Udayland.

The next line contains n integers a1, a2, ..., an (1≤ Ain, aii), a i denotes a road going from the town I to the town ai.

Output

Print a single integer-the number of ways to flip some set of the roads so, the resulting whole set of all roads are Not confusing. Since This number is too large, print the answer modulo 9 + 7.

Examples input
3
2 3 1
Output
6
Note

Consider the first sample case. There is 3 towns and 3 roads. The towns is numbered from 1 to 3 and the roads is, 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) is {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}. N Ote that the empty set is invalid because if no roads be flipped, then towns 1, 2, 3 was form a directed cycle, so it is confusing. Similarly, flipping all roads is confusing too. Thus, there is 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 was not con Fusing.

Test Instructions :

n Plots of points

Give you n edge, a[i] means I point to a[i]

Now you can change the direction of certain sides, yes, there's no ring in the graph.

Ask how many options you have

Exercises

A total of 2^n

For this figure, we see it as a non-direction.

We need to understand that there may be a ring in a unicom block made up of multiple unicom blocks.

Then define a unicom fast on the ring point is num, this unicom block has all points, after we given the direction, using Num,all we can find out this link block does not exist ring scheme number

The answer, then, is the product of the number of solutions to which the link is fast.

 

#include <bits/stdc++.h>using namespacestd;#pragmaComment (linker, "/stack:102400000,102400000")#defineLS i<<1#defineRS ls | 1#defineMid ((LL+RR) >>1)#definePII pair<int,int>#defineMP Make_pairtypedefLong LongLL;Const Long LongINF =1e18;Const DoublePi = ACOs (-1.0);Const intN = 2e5+Ten, M = 1e6+ One, inf = 2e9, mod = 1e9+7;intN,MX =-1, F[n],al,num;intDeep[n],vis[n];vector<int>G[n];voidAddintUintv) {G[u].push_back (v);} ll Quick_pow (ll X,ll p) {if(!p)return 1; LL ans= Quick_pow (x,p>>1); Ans= ans*ans%MoD; if(P &1) ans = ans*x%MoD; returnans;}voidDfsintUintFaintDEP) {Al++; Deep[u]=DEP; Vis[u]=1;  for(inti =0; I < g[u].size (); ++i) {intto =G[u][i]; if(!vis[to]) DFS (to,u,dep+1);Else if(TO!=FA) num = (ABS (Deep[to]-deep[u]) +1); }}llinch[N];intMain () {LL ans=1; inch[0] =1; scanf ("%d",&N);  for(inti =1; i < N; ++i)inch[I] = 1LL *inch[I1] *2%MoD;  for(inti =1; I <= N; ++i) {scanf ("%d",&f[i]); Add (I,f[i]); add (f[i],i);}  for(inti =1; I <= N; ++i) {al= num =0; if(Vis[i])Continue; DFS (i,0,0); if(Al = =2) num =2; Ans= (ans * (inch[NUM]-2LL]% mod *inch[Al-num]) %MoD; } printf ("%i64d\n", (ans+mod)%MoD); return 0;}

Codeforces Round #369 (Div. 2) D. Directed Roads Dfs to find the number of points on the ring for a unicom block

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.