QOJ.ac

QOJ

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

#11046. Idempotent Functions

统计

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
About Issues

We understand that our problem archive is not perfect. If you find any issues with the problem, including the statement, scoring configuration, time/memory limits, test cases, etc.

You may use this form to submit an issue regarding the problem. A problem moderator will review your issue and proceed it properly.

STOP! Before you submit an issue, please READ the following guidelines:

  1. This is not a place to publish a discussion, editorial, or requests to debug your code. Your issue will only be visible by you and problem moderators. Other users will not be able to view or reply your issues.
  2. Do not submit duplicated issues. If you have already submitted one, please wait for an moderator to review it. Submitting multiple issues will not speed up the review process and might cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
  4. Be sure your issue is related to this problem. If you need to submit an issue regarding another problem, contest, category, etc., you should submit it to the corresponding page.

Active Issues 0

No issues in this category.

Closed/Resolved Issues 0

No issues in this category.