Source Code
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define FAST ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define INF INT_MAX
#define ff first
#define ss second
#define all(v) v.begin(),v.end()
#define nl "\n"
using namespace std;
const ll MOD  = 1e9+7;
const int mxN = 2e5+1;


int main()
{
    FAST
    //==================
    ll t = 1 ;
    //cin >> t;
    while(t--){
        ll n; cin >> n ;
        ll p[n+1], q[n+1];
        for(int i =1;i<=n;i++)cin >> p[i];
        for(int i =1;i<=n;i++)cin >> q[i];
        map<ll,int> m;
        bool is = 1;
        for(int i=1 ;i<=n;i++){
            ll k= ((q[i] - p[i])%i);
            if(k!=0){
                is =0;
                break;
            }else{
                ll k2 = (q[i] - p[i])/i+1;
                m[k2]++;
            }
        }
        ll ans =1 ;
        if(!is)ans=0;
        ll finished =0 ;
        int i =0 ;
        while(finished!=n){
            finished+= m[i];
            if(finished == n)break;
            ll k = n - finished;
            ans = ((ans%MOD) * (k%MOD))%MOD ;
            i++;
        }
        cout << ans + n << nl;



    }

}
Copy
N-dimensions Imam Droubi
GNU G++17
1351 ms
262.1 MB
Memory Limit Exceeded