#include<bits/stdc++.h>
#define LL long long
#define ull unsigned long long
#define ULL ull
#define mp make_pair
#define pii pair<int,int>
#define piii pair<int, pii >
#define pll pair <ll,ll>
#define pb push_back
#define big 20160116
#define INF 2147483647
#define pq priority_queue
using namespace std;
inline int read(){
int x=0,f=1;
char ch=getchar();
while (ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
namespace Mymath{
LL qp(LL x,LL p,LL mod){
LL ans=1;
while (p){
if (p&1) ans=ans*x%mod;
x=x*x%mod;
p>>=1;
}
return ans;
}
LL inv(LL x,LL mod){
return qp(x,mod-2,mod);
}
LL C(LL N,LL K,LL fact[],LL mod){
return fact[N]*inv(fact[K],mod)%mod*inv(fact[N-K],mod)%mod;
}
template <typename Tp> Tp gcd(Tp A,Tp B){
if (B==0) return A;
return gcd(B,A%B);
}
template <typename Tp> Tp lcm(Tp A,Tp B){
return A*B/gcd(A,B);
}
};
namespace fwt{
using namespace Mymath;
void FWT(int a[],int n,LL mod)
{
for(int d=1;d<n;d<<=1)
for(int m=d<<1,i=0;i<n;i+=m)
for(int j=0;j<d;j++)
{
int x=a[i+j],y=a[i+j+d];
a[i+j]=(x+y)%mod,a[i+j+d]=(x-y+mod)%mod;
//xor:a[i+j]=x+y,a[i+j+d]=x-y;
//and:a[i+j]=x+y;
//or:a[i+j+d]=x+y;
}
}
void UFWT(int a[],int n,LL mod)
{
LL rev=inv(2,mod);
for(int d=1;d<n;d<<=1)
for(int m=d<<1,i=0;i<n;i+=m)
for(int j=0;j<d;j++)
{
int x=a[i+j],y=a[i+j+d];
a[i+j]=1LL*(x+y)*rev%mod,a[i+j+d]=(1LL*(x-y)*rev%mod+mod)%mod;
//xor:a[i+j]=(x+y)/2,a[i+j+d]=(x-y)/2;
//and:a[i+j]=x-y;
//or:a[i+j+d]=y-x;
}
}
void solve(int a[],int b[],int n,LL mod)
{
FWT(a,n,mod);
FWT(b,n,mod);
for(int i=0;i<n;i++) a[i]=1LL*a[i]*b[i]%mod;
UFWT(a,n,mod);
}
};
using namespace Mymath;
const int Maxn=105;
const LL mod=1e9+7;
int a[Maxn],n,m;
LL v[Maxn];
LL dp[2][Maxn][Maxn][Maxn];
LL g[Maxn][Maxn];
void Add(LL &x,LL y){
x+=y;
x%=mod;
}
void mian(){
memset(dp,0,sizeof(dp));
n=read();m=read();
LL iv=inv(m,mod);
for (int i=1;i<=n;i++) a[i]=read();
for (int i=1;i<=m;i++) v[i]=read();
int s1=1,e1=m;
if (a[1]) s1=a[1],e1=a[1];
int s2=1,e2=m;
if (a[2]) s2=a[2],e2=a[2];
int s3=1,e3=m;
if (a[3]) s3=a[3],e3=a[3];
LL tt=1;
for (int i=1;i<=n;i++){
if (!a[i]) tt=tt*iv%mod;
}
for (int i=s1;i<=e1;i++){
for (int j=s2;j<=e2;j++){
for (int k=s3;k<=e3;k++){
dp[1][k][g[j][k]][g[i][g[j][k]]]++;
//cout<<i<<' '<<g[i][j]<<' '<<
}
}
}
for (int ii=3;ii<n;ii++){
int i=ii&1;
for (int j=1;j<=m;j++){
for (int k=1;k<=m;k++){
if(j%k!=0) continue;
for (int l=1;l<=m;l++){
if (k%l!=0) continue;
dp[i^1][j][k][l]=0;
}
}
}
for (int j=1;j<=m;j++){
for (int k=1;k<=m;k++){
if(j%k!=0) continue;
for (int l=1;l<=m;l++){
if (k%l!=0) continue;
int s=1,e=m;
if (a[ii+1]) s=a[ii+1],e=a[ii+1];
for (int p=s;p<=e;p++){
Add(dp[i^1][p][g[j][p]][g[k][p]],dp[i][j][k][l]*v[g[l][p]]);
}
}
}
}
}
LL ans=0;
for (int i=1;i<=m;i++){
for (int j=1;j<=m;j++){
for (int k=1;k<=m;k++){
ans+=dp[n&1][i][j][k];
ans%=mod;
}
}
}
printf("%I64d\n",ans*tt%mod);
}
int main(){
for (int i=0;i<Maxn;i++){
for (int j=0;j<Maxn;j++){
g[i][j]=__gcd(i,j);
}
}
int T;
scanf("%d",&T);
while (T--){
mian();
}
return 0;
}
[HDU 6327]Random Sequence
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.