Source Code
#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;
#define sz(s) (int)(s.size())
#define all(v) v.begin(),v.end()
#define clr(d,v) memset(d,v,sizeof(d))
#define ll long long
#define ld long double
#define ull unsigned long long
int dy[] = { 1, -1, 0, 0, -1, 1, 1, -1 };
int dx[] = { 0, 0, 1, -1, 1, -1, 1, -1 };
ll gcd(ll x, ll y) { return(!y) ? x : gcd(y, x % y); }
ll lcm(ll x, ll y) { return((x / gcd(x, y)) * y); }
void file()
{
    std::ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
}
const int N = 2e6 + 5;
ll factorial[N];
int mod = 1e9 + 7;

void fact()
{
    factorial[0] = factorial[1] = 1;
    for (int i = 2; i < N; i++)
    {
        factorial[i] = (factorial[i - 1] % mod * 1ll * i % mod) % mod;
    }

}
ll power(ll x, ll k)
{
    ll ret = 1;
    while (k) {
        if (k & 1) ret = (ret * x) % mod;
        k >>= 1; x = (x * x) % mod;
    }
    return ret;
}
ll modInverse(int a, int m=mod) {
    return power(a, m - 2);
}
int main()
{
    file();
    fact();
    int n;
    cin >> n;
    vector<ll>p(n);
    vector<ll>q(n);
    for (auto& it : p)
    {
        cin >> it;
    }
    for (auto& it : q)
        cin >> it;
    vector<ll>temp(n);
    bool valid = true;
    ll sum = 0;
    ll sum2 = 1;
    for (int i = 0; i < n; i++)
    {
        if (p[i] > q[i] || (q[i] - p[i]) % (i + 1) != 0)
        {
            valid = false;
            break;
        }
        temp[i] = (q[i] - p[i]) / (i + 1);
        sum += temp[i];
        sum2 = (sum2 % mod * factorial[temp[i]] % mod) % mod;
    }
   
    if (!valid)
    {
        cout << 0;
        return 0;
    }
    ll m = 1;
    for (ll i = 1; i <= sum; i++)
    {
        m = (m % mod * i % mod) % mod;
    }
 
    cout << (m % mod * modInverse(sum2) % mod) % mod;
}

 
Copy
N-dimensions Abo_Samrah
GNU G++17
218 ms
20.7 MB
Accepted