#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e5+10;
const int mod=998244353;
struct BIT{
int n,a[N];
int lowbit(int x){return x&(-x);}
void init(int _n){
n=_n;for(int i=1;i<=n;i++) a[i]=0;
}
void add(int pos,int x){
a[pos]+=x;
while((pos+=lowbit(pos))<=n){
a[pos]+=x;
}
}
int getsum(int x){
int sum=a[x];
while((x-=lowbit(x))>0){
sum+=a[x];
}
return sum;
}
}bit1;
int n,m,s[N],t[N],f[N],invf[N],inv[N];
int cnt[N];
void init(){
bit1.init(N-1);
f[0]=invf[0]=inv[1]=1;
for(int i=1;i<N;i++) f[i]=f[i-1]*i%mod;
for(int i=2;i<N;i++) inv[i]=(mod-mod/i)*inv[mod%i]%mod;
for(int i=1;i<N;i++) invf[i]=invf[i-1]*inv[i]%mod;
}
int32_t main(){
init();
scanf("%lld%lld",&n,&m);
for(int i=1;i<=n;i++)scanf("%lld",&s[i]),bit1.add(s[i],1),cnt[s[i]]++;
for(int i=1;i<=m;i++)scanf("%lld",&t[i]);
int res=0,tmp=f[n];
for(int i=1;i<N;i++) tmp=(tmp*invf[cnt[i]])%mod;
int i;
// cout<<tmp<<endl;
for(i=1;i<=m&&i<=n;i++){
int left=bit1.getsum(t[i]-1);
// cout<<left<<endl;
res=(res+left*tmp%mod*inv[n-i+1]%mod)%mod;
if(cnt[t[i]]==0) break;
tmp=tmp*f[cnt[t[i]]]%mod*invf[cnt[t[i]]-1]%mod;
tmp=tmp*inv[n-i+1]%mod;
cnt[t[i]]--;bit1.add(t[i],-1);
}
if(n<m&&i==n+1) res=(res+1)%mod;
cout<<res<<endl;
}
Codeforces Round #775 (Div. 1, based on Moscow Open Olympiad in Informatics) C. Tyler and Strings
最新推荐文章于 2022-12-10 23:11:31 发布