CF1466H.Finding satisfactory solutions

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Getting so far in this contest is not an easy feat. By solving all the previous problems, you have impressed the gods greatly. Thus, they decided to spare you the story for this problem and grant a formal statement instead.

Consider nn agents. Each one of them initially has exactly one item, ii -th agent has the item number ii . We are interested in reassignments of these items among the agents. An assignment is valid iff each item is assigned to exactly one agent, and each agent is assigned exactly one item.

Each agent has a preference over the items, which can be described by a permutation pp of items sorted from the most to the least desirable. In other words, the agent prefers item ii to item jj iff ii appears earlier in the permutation pp . A preference profile is a list of nn permutations of length nn each, such that ii -th permutation describes preferences of the ii -th agent.

It is possible that some of the agents are not happy with the assignment of items. A set of dissatisfied agents may choose not to cooperate with other agents. In such a case, they would exchange the items they possess initially ( ii -th item belongs to ii -th agent) only between themselves. Agents from this group don't care about the satisfaction of agents outside of it. However, they need to exchange their items in such a way that will make at least one of them happier, and none of them less happy (in comparison to the given assignment).

Formally, consider a valid assignment of items — AA . Let A(i)A(i) denote the item assigned to ii -th agent. Also, consider a subset of agents. Let SS be the set of their indices. We will say this subset of agents is dissatisfied iff there exists a valid assignment B(i)B(i) such that:

  • For each iSi \in S , B(i)SB(i) \in S .
  • No agent iSi \in S prefers A(i)A(i) to B(i)B(i) (no agent from the SS is less happy).
  • At least one agent iSi \in S prefers B(i)B(i) to A(i)A(i) (at least one agent from the SS is happier).

An assignment is optimal if no subset of the agents is dissatisfied. Note that the empty subset cannot be dissatisfied. It can be proven that for each preference profile, there is precisely one optimal assignment.

Example: Consider 33 agents with the following preference profile:

  1. [2,1,3][2, 1, 3]
  2. [1,2,3][1, 2, 3]
  3. [1,3,2][1, 3, 2]

And such an assignment:

  • First agent gets item 22
  • Second agent gets item 33 .
  • Third agent gets item 11 .

See that the set of agents {1,2}\{1, 2\} is dissatisfied, because they can reassign their (initial) items in the following way:

  • First agent gets item 22 .
  • Second agent gets item 11 .
  • Third agent gets item 33 .

This reassignment will make the second agent happier and make no difference to the first agent. As a result, the third agent got an item that is worse for him, but this does not prevent the set {1,2}\{1,2\} from being dissatisfied (he is not in this set).

The following assignment would be optimal:

  • First agent gets item 22 .
  • Second agent gets item 11 .
  • Third agent gets item 33 .

Given an assignment AA , calculate the number of distinct preference profiles for which assignment AA is optimal. As the answer can be huge, output it modulo 109+710^9+7 .

Two preference profiles are different iff they assign different preference permutations to any agent.

输入格式

In the first line of input there is an integer nn ( 1n401 \leq n \leq 40 ). The next line contains nn space separated integers, a permutation of numbers from 11 to nn . The ii -th number denotes the item assigned to agent ii in the optimal assignment.

输出格式

In a single line output one non-negative integer, the number of preference profiles for which the assignment of items given in the input is optimal modulo 109+710^9+7 .

输入输出样例

  • 输入#1

    2
    2 1

    输出#1

    1
  • 输入#2

    3
    1 2 3

    输出#2

    98
  • 输入#3

    4
    2 1 3 4

    输出#3

    27408

说明/提示

Assignment from the first test case is optimal only for the following preference profile:

2,12, 1

1,21, 2

If any agent wants his initial item the most and is given another item, he would form a dissatisfied set. Hence the allocation is not optimal for any other preference profile.

首页