Source Code
#include<iostream>
#include<vector>
#include<cmath>
#include<algorithm>
#include<map>
#include<cstring>
#include<sstream>
using namespace std;
long long mod=1000000007,mx=0;
long long factorial[3000000];
long long   Fpow(long long x, long long  y,long long  mod) {
    return(!y ? 1 : (((y & 1) ? x : 1) % mod * Fpow((x % mod) * (x % mod) % mod, y >> 1, mod) % mod) % mod);
}
void GetFactorial(){
    long long ans=1;
    factorial[0]=1;
    for(long long  i=1;i<=2472179;i++){

        ans=((ans%mod)*(i%mod))%mod;
        factorial[i]=ans;
    }
}
int main() {
    std::ios_base::sync_with_stdio(NULL);
    cin.tie(NULL);
    cout.tie(NULL);
    long long n, o = 0, ans = 1, m = 0, in = -1;
    cin >> n;
    vector<long long> p(n + 1), q(n + 1);
    GetFactorial();
    for (int i = 1; i <= n; i++)
        cin >> p[i];
    bool valid=true;
    for(long long  i=1;i<=n;i++)
    {
        cin>>q[i];
        o+=(q[i]-p[i])/i;
        if(q[i]<p[i]||(q[i]-p[i])%i!=0)valid=false;
    }
    long long b;
    if(!valid)o=0;
    mx = factorial[o];
    ans=mx;
    if(valid) {
        for (int i = 1; i <= n; i++) {

            b = (q[i] - p[i]) / i;
            b = factorial[b];
            if (b != 0)
                ans = ((ans % mod) * Fpow(b, mod - 2, mod) % mod) % mod;
        }
    }
    if(!valid)ans=0;
    cout <<ans;
}
Copy
N-dimensions AyaHamza
GNU G++17
442 ms
22.8 MB
Accepted