HCPC Team Formation
Time limit1sMemory limit1024 MB
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 from people who are deciding whether to join HCPC.
The people are numbered from to . 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 can form a team. Write a program that counts the number of ways to form a team of for Dongmin. Since the count can be very large, print it modulo .
Input
The first line contains an integer , the number of people.
The second line contains integers separated by spaces. If , person has no preferred partner. Otherwise, is the number of the person that person wants to be with.
Output
Print the number of ways to choose of the people to form one team, modulo .