Source Code
#include <bits/stdc++.h>

using namespace std;

#define fast ios::sync_with_stdio(NULL); cin.tie(0); cout.tie(0);

typedef long long ll;

const ll N = 2e5 + 11, M = 2e6 + 11, mod = 1e9 + 7;

ll n, sum, ans = 1, a[N], b[N], fact[M], factM[M];

ll power(ll x , ll y , ll m)
{
    if(y == 0) return 1;
    ll p = power(x , y / 2 , m) % m;
    p = (p * p) % m;
    return (y % 2 == 0) ? p : (x * p) % m;
}


ll modInverse(ll a , ll m)
{
    return power(a , m - 2 , m);
}

void build()
{
    fact[0] = factM[0] = 1;
    for(int i = 1; i < M; i++)
    {
        fact[i] = (i * fact[i - 1]) % mod;
        factM[i] = modInverse(fact[i], mod);
    }
}

ll nCr(ll n, ll r)
{
    return (((fact[n] * factM[r]) % mod) * factM[n - r]) % mod;
}


int main()
{
    build();
    fast
    cin >> n;
    for(int i = 1; i <= n; i++) cin >> a[i];
    for(int i = 1; i <= n; i++) cin >> b[i];
    for(int i = 1; i <= n; i++)
    {
        if(a[i] > b[i] || (b[i] - a[i]) % i != 0) return cout << 0, 0;
        a[i] = (b[i] - a[i]) / i;
        sum += a[i];
    }
    for(int i = 1; i <= n; i++)
    {
        ans = (ans * nCr(sum, a[i])) % mod;
        sum -= a[i];
    }
    cout << (ans + mod) % mod << endl;
}
Copy
N-dimensions Mohamad_Abodan
GNU G++17
1594 ms
34.8 MB
Runtime Error