Source Code
#include <bits/stdc++.h>
 
#define ll long long
#define mk make_pair
#define pb push_back
#define f first
#define s second
using namespace std;
 
ll mod = 1e9 + 7 ;
 
const int N = 2e6 + 10 ;
 
ll fact[N] ;
ll mul(ll a , ll b){
    return a * b % mod ;
}
ll power(ll a , ll b){
    ll ans = 1 , cur = a;
    while(b){
        if(b%2) ans = mul(ans , cur) ;
        cur = mul(cur , cur) ;
        b /= 2;
    }
    return ans ;
}
ll divide(ll a , ll b){
    return mul(a , power(b , mod - 2)) % mod ;
}
ll nck(ll a , ll b){
    return divide(fact[a] , fact[b] * fact[a-b] % mod) % mod ;
}
 
 
int main(){
	
	int n ; 
	cin >> n ;
	ll sum = 0 ; 
	vector<ll> steps(n) , p(n) , q(n) ;
	for(int i=0 ; i<n ; i++) scanf("%lld" , &p[i]) ; 
	for(int i=0 ; i<n ; i++){
		scanf("%lld" , &q[i]);
		ll need = q[i] - p[i] ; 
		if(need < 0 || (need % (i + 1) != 0)){
			return cout << 0,0 ;
		}
		steps[i] = need / (i + 1) ;
		sum += steps[i] ;
	}

	fact[0] = fact[1] = 1 ; 
	for(int i=2 ; i<N ; i++){
		fact[i] = (fact[i - 1] * i) % mod ; 
	}
	ll ans = fact[sum] ;
	for(int i=0 ; i<n ; i++){
		ans = divide(ans , fact[steps[i]]) ;
	}
	cout << ans;

 
 
	return 0 ;
}
Copy
N-dimensions Tuff1aha
GNU G++17
253 ms
20.6 MB
Runtime Error