Source Code
#include <bits/stdc++.h>
using namespace std;
const int N = 4e6 + 9;
const int mod = 1e9 + 7;
int gcdExtended(long long a, long long b, long long *x, long long *y)
{
    // Base Case
    if (a == 0)
    {
        *x = 0, *y = 1;
        return b;
    }
 
    long long x1, y1; // To store results of recursive call
    long long gcd = gcdExtended(b%a, a, &x1, &y1);
 
    // Update x and y using results of recursive
    // call
    *x = y1 - (b/a) * x1;
    *y = x1;
 
    return gcd;
}
long long modInverse(long long b , long long mod){
    long long x , y;
    long long d= gcdExtended(b , mod , &x , &y);
    if(d != 1) return -1;
    return (x%mod + mod ) % mod;
}
long long modDivide(long long a,long long b, long long m)
{
    a = a % m;
    long long inv = modInverse(b, m);
    if (inv == -1)
        return -1;
    else
        return (inv * a) % m;
}
int fact[N];
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    int n;
    cin >> n;
    int a[n] , b[n];
    fact[0] = fact[1] = 1;
    for(int i = 2; i < N; i++){
        fact[i] = fact[i - 1] * i;
        fact[i] %= mod;
    }
    for(int i = 0; i < n; i++) cin >> a[i];
    for(int i = 0; i < n; i++) cin >> b[i];
    int arr[n];
    long long sum = 0;
    int test=  1;
    for(int i = 0; i < n; i++){
        arr[i] = (b[i] - a[i]) / ( i + 1 );
        if((b[i] - a[i]) % ( i + 1 )) test = 0;
        sum += arr[i];
    }
    if(!test){
        cout << 0 << "\n";
        return 0;
    }
    long long ans = fact[sum];
    for(int i = 0; i < n; i++){
        long long e = modDivide(ans, fact[arr[i]], mod);
        ans = e;
    }
    cout << ans << "\n";
    return 0;
}  
Copy
N-dimensions AbduSaber
GNU G++17
98 ms
18.4 MB
Runtime Error