Codeforces 272D。Dima and Two Sequences,多重集的全排列

output

standard output

Little Dima has two sequences of points with integer coordinates: sequence (a1, 1), (a2, 2), ..., (an, n) and sequence (b1, 1), (b2, 2), ..., (bn, n).

Now Dima wants to count the number of distinct sequences of points of length 2·n that can be assembled from these sequences, such that the x-coordinates of points in the assembled sequence will not decrease. Help him with that. Note that each element of the initial sequences should be used exactly once in the assembled sequence.

Dima considers two assembled sequences (p1, q1), (p2, q2), ..., (pn, qn) and (x1, y1), (x2, y2), ..., (xn, yn) distinct, if there is such i (1 ≤ i ≤ 2·n), that (pi, qi) ≠ (xi, yi).

As the answer can be rather large, print the remainder from dividing the answer by number m.

Input

The first line contains integer n (1 ≤ n ≤ 105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109). The third line contains nintegers b1, b2, ..., bn (1 ≤ bi ≤ 109). The numbers in the lines are separated by spaces.

The last line contains integer m (2 ≤ m ≤ 109 + 7).

Output

In the single line print the remainder after dividing the answer to the problem by number m.

Examples

input

Copy

1
1
2
7

output

Copy

1

input

Copy

2
1 2
2 3
11

output

Copy

2

Note

In the first sample you can get only one sequence: (1, 1), (2, 1).

In the second sample you can get such sequences : (1, 1), (2, 2), (2, 1), (3, 2); (1, 1), (2, 1), (2, 2), (3, 2). Thus, the answer is 2.

这个题一开始没看出来相同的最多只有两个然后就是我直接打表给我坑死了我想后来直接统计每个区间中重复的个数直接给他快速幂一除就好了呢结果是得把阶乘拆开一个个的算我佛了一上午就肛在这个地方了(中间吧map都用了本来以为是计数的问题结果不是)

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <bits/stdc++.h>
#define MAXN 20
#define INF 0x3f3f3f3f
 
using namespace std;
long long m;
long long ji[200100];
void jie(int m)
{
    ji[0]=ji[1]=1;
    for(int i=2;i<=200000;i++)
        ji[i]=i*ji[i-1]%m;
    return ;
}
 
struct ju
{
    int l,r;
}a[200220];
bool cmp(ju a,ju b)
{
    return a.l<b.l;
}
long long kuais(int a,int b)
{
    long long ans=1;
    for(;b;b>>=1)
    {
        if(b&1)
            ans=ans*a;
        a*=a;
    }
    return ans;
}
int main()
{
int n;
    while(cin>>n)
    {
        memset(a,0,sizeof(a));
        long long x,ans=1;
        for(int i=1;i<=n;i++)
        {
            cin>>x;
            a[i].l=x,a[i].r=i;
        }
        for(int i=1;i<=n;i++)
        {
            cin>>x;
            a[n+i].l=x,a[n+i].r=i;
        }
        cin>>m;
        jie(m);
        sort(a+1,a+1+2*n,cmp);
        a[2*n+1].l=-1,a[2*n+1].r=-1;
        map<int,int>jk;
        jk.clear();
        for(int i=1;i<=2*n;i++)
        {
            jk[a[i].r]++;
            if(a[i].l!=a[i+1].l)
            {
                int sam=0,sum=0;
                for(map<int,int>::iterator ll=jk.begin();ll!=jk.end();ll++)
                {
                    sum+=ll->second;
                    //cout<<ll->first<<' '<<ll->second<<' '<<endl;
                    if(ll->second>=2)
                    {
                        sam++;
                    }
                }
                queue<int>sb;
                for(int j=1;j<=sum;j++)
                {
                    if(j%2==0)
                    {
                        if(sam)
                        {
                            sam--;
                            sb.push(j/2);
                        }
                        else
                            sb.push(j);
                    }
                    else
                        sb.push(j);
                }
                while(!sb.empty())
                {
                    long long gh=sb.front();
                    sb.pop();
                    ans=ans*gh%m;
                }
                jk.clear();
            }
        }
        cout<<(ans)%m<<endl;
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值