Source Code
#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#include <unordered_map>
//#include"testlib.h"
#define endl '\n'
#define all(v) v.begin(),v.end()
#define allr(s) s.rbegin(),s.rend()
#define RT(s) return cout<<s,0
#define sz(s)    (int)(s.size())
//#define PI acos(-1)
#define EPS 1e-8
#define watch(x) cout << (#x)<<" = "<<x<<endl
#define mk(x, y) make_pair(x, y)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
int dy[] = { 1, -1, 0, 0, -1, 1, 1, -1 };
int dx[] = { 0, 0, 1, -1, 1, -1, 1, -1 };
void file() {
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	//freopen("C:\\Users\\karemo\\source\\repos\\generator\\generator\\out.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
#else
	//freopen("gcd.in", "r", stdin);
	//freopen("out.txt", "w", stdout);
#endif
}


void fast() {
	std::ios_base::sync_with_stdio(0);
	cin.tie(NULL);
}
int mod = 1e9 + 7;
const int N = 4e6;
int fact[N], inv[N];
int fpow(int a, int b) {
	a %= mod;
	int res = 1;
	while (b) {
		if (b & 1)
			res = res * 1LL * a % mod;
		a = a * 1LL * a % mod;
		b >>= 1;
	}
	return res;
}
int main() {
	file();
	fast();
	fact[0] = fact[1] = 1;
	for (int i = 2; i < N; i++)
		fact[i] = fact[i - 1] * 1LL * i % mod;
	for (int i = 0; i < N; i++)
		inv[i] = fpow(fact[i], mod - 2);
	int n; cin >> n;
	vi a(n + 1), b(n + 1);
	for (int i = 1; i <= n; i++)
		cin >> a[i];
	for (int i = 1; i <= n; i++)
		cin >> b[i];
	int bst = 0;
	for (int i = 1; i <= n; i++) {
		if (a[i] > b[i]) RT("0\n");
		if ((b[i] - a[i]) % i) RT("0\n");
		bst += (b[i] - a[i]) / i;
	}
	int ans = fact[bst];
	for (int i = 1; i <= n; i++) {
		ans = ans * 1LL * inv[(b[i] - a[i]) / i] % mod;
	}
	cout << ans << endl;
	return 0;
}
Copy
N-dimensions karemo
GNU G++17
2932 ms
33.2 MB
Accepted