F - Random Sort (18.9.30)(全排问题,取模问题)

Statements

Saeed is teaching Algorithms 1 at Damascus University, his last lecture was about sorting algorithms. As homework, he gave the students an array A of length n and asked them to sort it in increasing order.

Shahhoud fell asleep during the lecture, so he doesn't know how to sort the array. He decided to create his own algorithm, Random Sort. He first chooses a permutation p of length n (A permutation of length n is an array of length n where each integer between 1 and n appears exactly once).

After that, he decides that the sorted array is Api for each 1 ≤ i ≤ n.

In other words sorting the array A1, A2, ..., An results in the array Ap1, Ap2, ... , Apn

This algorithm does not always sort the array correctly. Shahhoud is wondering about the number of ways he can choose a permutation p that results in a correctly sorted array. Since the number can be very large, he wants to calculate it modulo 7901. Can you help him?

Input

The first line contains a single integer T, the number of test cases.

Each test case starts with a single line that contains an integer n, the length of the array A. (1 ≤ n ≤ 1000)

The next line contains n space-separated integers Ai, the array that needs to be sorted. (1 ≤ Ai ≤ 1000)

Output

For each test case, print one line containing one integer, the number of ways of choosing a permutation p that correctly sorts the array A in increasing order. Print the answer modulo 7901.

Example

Input

2
3
1 2 3
2
5 5

Output

1
2

Note

Two permutations P and Q are considered different if Pi ≠ Qi for any 1 ≤ i ≤ n

题意:给n个数,让我们排成上升的系列,问有多少种排法;

思路:不同的数,我们不要管,因为他在上升系列中的位置是固定的,所以,我们只要考虑n个数中,有多少个相同的是,然后全排,在相乘就行,要注意的是,每次都要取模mod;

看代码:

#include <iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
const int mod=7901;
int a[1010];
int f(int x)
{
    int res=1;
    for(int i=1; i<=x; i++)
    {
        res=res*i;
        res%=mod;
    }
    return res;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        int x;
        memset(a,0,sizeof(a));
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&x);
            a[x]++;
        }
       int ans=1;
        for(int i=1; i<=1000; i++)
        {
            if(a[i]!=0)
            {
                ans=ans*f(a[i]);
                ans%=mod;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值