QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 64 MB Total points: 100

#11046. Idempotent Functions

Statistics

You are given a positive integer $ n $. By $ A $ we mean the set $\{1, 2, 3, \ldots, n\}$. Function $ f : A → A $ is called a permutation if it is injective (for distinct arguments returns distinct values). Function $ f : A → A $ is called idempotent if for every $ i ∈ A $ holds $ f(f(i)) = f(i) $.

You are given a function $ f : A → A $. How many pairs of functions $(g, h)$ satisfy following conditions:

  • $ g : A → A $ is a permutation,
  • $ h : A → A $ is idempotent,
  • for every $ i ∈ A $ holds $ f(i) = h(g(i)) $?

Write a program which:

  • reads number $ n $ and description of function $ f $ from the standard input ,
  • determines the number of pairs of functions $(g, h)$ satisfying the requirement described above,
  • writes the result to the standard output.

Input Format

In the first line of input there is one integer $ n $ ($1 ≤ n ≤ 200\,000$). Second line contains a description of function $ f : f(i)$ ($1 ≤ f(i) ≤ n$) for $ i = 1, \ldots, n $, separated with single spaces.

Output Format

In the first line of output there should be a single integer: the remainder of the division by $1\,000\,000\,007$ of the number of different pairs of functions $(g, h)$ satisfying the requirement.

Example

Input

8
7 4 5 1 7 4 4 1

Output

288

Discussions

About Discussions

The discussion section is only for posting: Editorials, General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues. Submitting multiple issues may cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.