cswork / OAOpen practice workspace

Geneva TradingOA #1

Minimum Number of Permutation Operations

Estimated MediumJudge-ready

Topics (AI-labelled)Bits & MathUnion-Find

Given a permutation p of 1..n. One operation simultaneously sets temp[i]=arr[p[i]] for every 1-based index i, and then sets arr=temp. For an initial array of any n distinct elements, find the minimum number of operations needed to return to the initial array, modulo 1,000,000,007.

This site adds the standard input/output protocol.

Input

n; the second line contains the n permutation values p[i].

Output

Output the minimum number of operations modulo 1,000,000,007.

Example 1

Input:

3
1 3 2

Output:

2

Example 2

Input:

4
2 1 4 3

Output:

2

Example 3

Input:

5
2 3 1 5 4

Output:

6

Editorial

Loading solution…