#include <stdio.h>
#include <math.h>
#include <iostream>
#include <string.h>
#include <vector>
#define LL long long
#define MOD 1000000007
#define MAX(a,b) ((a>b)?a:b)
using namespace std;
/*
	Fast input via getchar_unlocked
*/
inline void fastInt(int &a){
	char c;
	while(c<33 || c=='-'){
		 c=getchar_unlocked();
	}
	a=0;
	while(c>33){
		a=(a*10)+c-'0';
		c=getchar_unlocked();
	}
}
int visited[1000010]; //Prunning
int p[1000010]; //Pi's 
int r[1000010]; //Lenghts of the cycles
int primes[80000]; //Primes below 10^6
int ind[1000010]; //Index of a prime number i in the array "primes"
bool sieve[1000010]; //Prime or Not? Array
int tamp; //Amount of primes 
/* Sieve of Eratosthenes to obtain primes below 10^6 */
void fill_sieve(){
	memset(sieve, true, sizeof(sieve));
	for(int i=2; i<(int)sqrt(1000000); i++){
		if(!sieve[i]) continue;
		for(int j=i; j*i<=1000000; j++){
			sieve[i*j] = false;
		}
	}
	tamp = 0;
	sieve[0] = sieve[1] = false;
	for(int i=0; i<=1000000; i++) {
		if(sieve[i]){ 
			primes[tamp++] = i; //Storing the primes and their respective indexes
			ind[i] = tamp-1;
		} 
	}
}
int pot[80000]; //Power of a prime factor
/*
	There are less than 80000 primes below 10^6, so
	I used an array of that size to store them!
*/
/* Trial division to obtain the prime factorization of "num"*/
int K;
void f(int num){
	if(sieve[num]){
		pot[ind[num]] = MAX(pot[ind[num]], 1);
		return;
	}
	int cont;
	cont = 0;
	int idx=0;

	while(primes[idx]*primes[idx]<=num){
		if (num%primes[idx]==0){
			num/=primes[idx];
			visited[num] = K+1;            //Don't factorize the same number again
			cont++;
			pot[idx] = MAX(pot[idx], cont);
		}else{
			cont = 0;
			idx++;
		}
	}
	if (num>1){ 
		if(primes[idx]==num) pot[ind[num]] = MAX(pot[ind[num]], cont+1);
		else pot[ind[num]] = MAX(pot[ind[num]], 1);
	}	
}
/*
	Fast Exponentiation
*/
LL fpow (LL a, LL b){

	LL ans=1LL;
	while (b){
		if (b&1) ans= (ans*a)%MOD;
		a= (a * a) %MOD;
		b>>=1;
	}
	
	return ans;
}
int done[1000010]; //Length already stored?
int main(){
	fill_sieve();
	int n;
	int T= 0;
	K = 0;
	while(scanf("%d", &n)!=-1){
		for(int i=1; i<=n; i++) fastInt(p[i]);
		int tam = 0;
		/*
			Disjoint cycle finding
			Iterate over all Pi, if a position is not visited,
			start counting the cycle until we reach a already
			visited position
		*/
		for(int i=1; i<=n; i++){
			if(visited[i]!=K+1){
				r[tam] = 0;
				int P = i;
				while(visited[P]!=K+1){
					visited[P] = K+1;
					r[tam]++;
					P = p[P];
				}
				if(done[r[tam]]!=T+1){    //Don't store a number previously stored
					done[r[tam]] = T+1;
					tam++;
				}
			}
		}
		K++;
		memset(pot, 0, sizeof(pot)); //Set to 0 all the power of the prime numbers
		for(int i=0; i<tam; i++){
			if(visited[r[i]]==K+1) continue; //If the length is already factorized, don't do it again
			visited[r[i]] = K+1;
			f(r[i]);
		}
		K+=2;
		LL x = 1LL;
		for(int i=0; i<tamp; i++){
			LL X = fpow(primes[i], pot[i]); //Obtaining the LCM via prime powers multiplication
			x = (x*X)%MOD; //Modular multiplication (a*b)%p = ((a%p)*(b%p))%p to avoid overflow
		}
		T++;
		printf("%lld\n", x);
	}
	return 0;
}
