Under construction

Still being built. Things may change

HCPC Team Formation

Time limit1sMemory limit1024 MB

Summary
Count the 3-person teams where every member's preferred partner, if they have one, is also on the team.
Level

Medium6 of 10

Topics
Combinatorics, Graph, Implementation
Solved
No attempts yet

Problem

Dongmin wants to form a team of 33 from NN people who are deciding whether to join HCPC.

The people are numbered from 11 to NN. Some of them join only if they are on a team with the one person they want. Others have no preference and can join with anyone.

Dongmin has surveyed each person to find out whether they have a preferred partner and, if so, who it is. He has not yet worked out which groups of 33 can form a team. Write a program that counts the number of ways to form a team of 33 for Dongmin. Since the count can be very large, print it modulo 109+710^9+7.

Input

The first line contains an integer NN, the number of people. (1≤N≤500 000)(1 \le N \le 500\,000)

The second line contains NN integers A1,A2,⋯ ,ANA_1, A_2, \cdots, A_N separated by spaces. If Ai=iA_i = i, person ii has no preferred partner. Otherwise, AiA_i is the number of the person that person ii wants to be with. (1≤Ai≤N)(1 \le A_i \le N)

Output

Print the number of ways to choose 33 of the NN people to form one team, modulo 109+710^9+7.

Examples1

  1. Example 1

    Input
    5
    2 1 1 4 5
    
    Expected output
    3