2015
05-23

# Three Eggache God

Alpheus has n monsters,their heights are A1,A2,A3….An. God Kufeng has n sons,their heights are 1,2,3….n.
One day, Our daredevil God Kufeng wants to challenge Alpheus. He sends his n sons to fight with Alpheus’s n monsters ,the i-th son fighting with the i-th monster. During one challenge,if the i-th son’s height is equal or greater than the i-th monster’s height,the i-th son will get one score for his father,else,he’ll get nothing.
Since Alpheus is strong enough to accept an infinity number of challenges,God Kufeng can challenge Alpheus again and again,but he has to change his son’s order after every challenge,and the same permutation can not appear more than once.(that is,God Kufeng has n! times to challenge Alpheus).
Now,God Wu–the god of universe,wants to make things difficult to Kufeng.He has n sons too,their heights are W1,W2,W3….Wn(it is a permutation of 1,2,3…..n),God Wu said to Kufeng: Only when the lexicographic order of your sons’ heights is bigger than my son’s,you can challenge my lovely Alpheus!
Question:What is the total score God Kufeng can get?Output the answer mod 1000000007.
Note:
Alpheus will never change his monster’s order,God Wu will never change his son’s order.
For two permutation A and B,if Ai=Bi for all i(1<=i<=n),we say A=B.else,we get the smallest i that Ai!=Bi,if Ai>Bi,we say A’s lexicographic order is bigger than B,otherwise,B is bigger than A.

First,there is an integer T(T<=20),the number of test cases.
For every case,there are three lines.
The first line is an integer n(1<=n<=2000),the number of sons of God Kufeng.
The second line has n integers,W1,W2,W3…..Wn,the heights of God Wu’s sons.(we guarantee that W1,W2,W3…..Wn is a permutation of 1,2,3….n).
The third line also has n integers, A1,A2,A3….An(1<=Ai<=n),the heights of Alpheus’s monsters.

First,there is an integer T(T<=20),the number of test cases.
For every case,there are three lines.
The first line is an integer n(1<=n<=2000),the number of sons of God Kufeng.
The second line has n integers,W1,W2,W3…..Wn,the heights of God Wu’s sons.(we guarantee that W1,W2,W3…..Wn is a permutation of 1,2,3….n).
The third line also has n integers, A1,A2,A3….An(1<=Ai<=n),the heights of Alpheus’s monsters.

2
3
3 1 2
1 2 3
3
3 2 1
1 1 1

Case #1: 2
Case #2: 0

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>

#define MOD 1000000007
#define N 2010

using namespace std;
int n,ans;
int a[N],w[N];
int f[N][N];
int fac[N];
void init(){
fac[0]=1;
int i,j;
for (i=1;i<=n;i++)
fac[i]=(1ll*fac[i-1]*i)%MOD;
for (i=0;i<=n;i++) f[1][i]=n-i;
for (i=1;i<=n;i++){
for (j=0;j<w[i];j++) f[i+1][j]=f[i][j]-1;
for (j=w[i];j<=n;j++) f[i+1][j]=f[i][j];
}
}
int dfs(int k){
if (k==n) return 0;
int tp=(1ll*fac[n-k]*f[k][w[k]])%MOD;
int tp1=dfs(k+1);
if (w[k]>=a[k]) ans=(1ll*ans+tp1)%MOD;
return (1ll*tp1+tp)%MOD;
}
void work(){
int i,j,k;
for (i=1;i<=n;i++){
if (a[i]>w[i]) ans=(1ll*ans+1ll*f[i][a[i]-1]*fac[n-i])%MOD;
else ans=(1ll*ans+1ll*f[i][w[i]]*fac[n-i])%MOD;
//printf("%d\n",ans);
for (j=i+1;j<=n;j++){
k=f[i][w[i]]*f[i][a[j]-1]-min(f[i][w[i]],f[i][a[j]-1]);
//printf("%d %d %d %d\n",i,j,k,fac[n-i-1]);
ans=(1ll*ans+1ll*k*fac[n-i-1])%MOD;
//printf("%d\n",ans);
//system("pause");
}
}
}
int main(){
int test;
scanf("%d",&test);
for (int cas=1;cas<=test;cas++){
scanf("%d",&n);
for (int i=1;i<=n;i++)
scanf("%d",&w[i]);
for (int i=1;i<=n;i++)
scanf("%d",&a[i]);
ans=0;
init();
work();
//printf("%d\n",ans);
dfs(1);
printf("Case #%d: %d\n",cas,ans);
}
return 0;
}