HTML & JS
#include<bits/stdc++.h>
using namespace std;
#define mod 1000000007
typedef unsigned  long long ll;
ll fast(ll a,ll b){
if(b==0)return 1;
if(b&1)return (a*fast(a,b-1))%mod;
ll x=fast(a,b/2)%mod;
return (x*x)%mod;
}
vector<ll>a,b;
ll n,m;
ll p=0,q=1;
void cal(int i,ll z1,ll z2){
if(i==n)return ;
if(a[i]==0&&b[i]!=0){
p=((p*(m*z2))%mod+q*((m-b[i])*z1)%mod)%mod;
q=(q*z2*m)%mod;
z2=(z2*m)%mod;
cal(i+1,z1,z2);
}
if(a[i]!=0&&b[i]==0){
    p=(((p*m*z2)%mod+(q*(a[i]-1))*z1)%mod)%mod;
    q=(q*m*z2)%mod;
    z2=(z2*m)%mod;
    cal(i+1,z1,z2);
}
if(a[i]!=0&&b[i]!=0){
    if(a[i]>b[i]){
        p=(p*z2%mod+q*z1%mod)%mod;
        q=(q*z2)%mod;
    }
    if(a[i]==b[i])
        cal(i+1,z1,z2);
        else return ;
}
if(a[i]==0&&b[i]==0){
p=(((((p*m)%mod*m)%mod)*z2)%mod+((q*(m-1+mod)%mod)*z1)%mod)%mod;
q=(((((((q*m)%mod)*m)%mod)*z2)%mod)*2)%mod;
cout<<p<<' '<<q<<'\n';
z2=(z2*m)%mod;
cal(i+1,z1,z2);
}
return ;}
int main(){
    cin>>n>>m;
    a=vector<ll>(n);
    b=vector<ll>(n);
    for(int i=0;i<n;i++)cin>>a[i];
    for(int i=0;i<n;i++)cin>>b[i];
    cal(0,1,1);
    cout<<((p)*(fast(q,mod-2))%mod)%mod;
return 0;}

Generated URL:

Output:

99 20000
100980000 999999727
929286287 392
949999180 999945127
353430 768320000
607597763
Report Bug