Yatin created an interesting problem for his college juniors. Can you solve it?
Given N rooms, where each room has a one-way door to a room denoted by room[i], where 1≤i≤N. Find a positive integer K such that, if a person starts from room i, (1<=i<=N) , and continuously moves to the room it is connected to (i.e. room[i]) , the person should end up in room i after K steps;
Note: The condition should hold for each room.If there are multiple possible values of K modulo (109+7), find the smallest one.If there is no valid value of K, output −1
Constraints:
1≤T≤10
1≤N≤105
1≤room[i]≤N
Input Format
Output Format
Given array Room=[2,3,4,1]
If a person starts from
Room1
Path would be 1>2>3>4>1 , so person took 4 steps to reach room 1
Room2
Path would be 2>3>4>1>2 , so person took 4 steps to reach room 1
and so on for other rooms.
Similarly, it would give a value 4 for each case, And this is the smallest valid value, so K=4 for this case