Source Code
#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
#define mod_combine(a, b, m) (((a % m) * (b % m)) % m)
using namespace std;
int mod=1e9+7;
ll fact[10000005];
ll power(ll b, ll e, ll MOD)
{
    ll power = 1;
    while(e)
    {
        if(e & 1) power = mod_combine(power, b, MOD);
        e >>= 1, b = mod_combine(b, b, MOD);
    }
    return power % MOD;
}

ll inverse(ll N, ll MOD)
{
    return power(N, MOD - 2, MOD) % MOD;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    fact[0]=1;
    for(int i=1; i<=1e7; i++)fact[i]=(fact[i-1]*i)%mod;
    int n;
    ll cnt=0,x=1;
    cin>>n;
    vector<ll>p(n),q(n);
    for(auto&it:p)cin>>it;
    for(auto&it:q)cin>>it;
    for(int i=0; i<n; i++)
    {
        int num=(q[i]-p[i]);
        if(num%(i+1)!=0)return cout<<0,0;
        num/=(i+1);
        cnt+=num;
        x = ((x % mod) * (inverse(fact[num],mod)% mod)) % mod;
    }
    cnt=((fact[cnt]%mod)*(x%mod))%mod;
    cout<<cnt<<endl;
}
Copy
N-dimensions Amrharb
GNU G++17
285 ms
81.8 MB
Runtime Error