CF398E.Sorting Permutations

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

We are given a permutation sequence a1,a2,...,ana_{1},a_{2},...,a_{n} of numbers from 11 to nn . Let's assume that in one second, we can choose some disjoint pairs (u1,v1),(u2,v2),...,(uk,vk)(u_{1},v_{1}),(u_{2},v_{2}),...,(u_{k},v_{k}) and swap all auia_{ui} and avia_{vi} for every ii at the same time ( 1<=u_{i}<v_{i}<=n ). The pairs are disjoint if every uiu_{i} and vjv_{j} are different from each other.

We want to sort the sequence completely in increasing order as fast as possible. Given the initial permutation, calculate the number of ways to achieve this. Two ways are different if and only if there is a time tt , such that the set of pairs used for swapping at that time are different as sets (so ordering of pairs doesn't matter). If the given permutation is already sorted, it takes no time to sort, so the number of ways to sort it is 11 .

To make the problem more interesting, we have kk holes inside the permutation. So exactly kk numbers of a1,a2,...,ana_{1},a_{2},...,a_{n} are not yet determined. For every possibility of filling the holes, calculate the number of ways, and print the total sum of these values modulo 10000000071000000007 (109+7)(10^{9}+7) .

输入格式

The first line contains two integers nn (1<=n<=105)(1<=n<=10^{5}) and kk (0<=k<=12)(0<=k<=12) . The second line contains the permutation sequence a1,...,ana_{1},...,a_{n} (0<=ai<=n)(0<=a_{i}<=n) . If a number is not yet determined, it is denoted as 00 . There are exactly kk zeroes. All the numbers aia_{i} that aren't equal to zero are distinct.

输出格式

Print the total sum of the number of ways modulo 10000000071000000007 (109+7)(10^{9}+7) .

输入输出样例

  • 输入#1

    5 0
    1 5 2 4 3
    

    输出#1

    6
    
  • 输入#2

    5 2
    1 0 2 4 0
    

    输出#2

    7
    
首页