#define rep(i,n) for(int i=0;i<(int)(n);i++) #define ALL(v) v.begin(),v.end() typedef long long ll; #include using namespace std; const int MOD=1e9+7; int main(){ ios::sync_with_stdio(false); std::cin.tie(nullptr); int n; cin>>n; vector A(n),B(n); rep(i,n) cin>>A[i]; rep(i,n) cin>>B[i]; vector> C(n); rep(i,n) C[i]={(double)(B[i]-1)/A[i],i}; sort(ALL(C)); reverse(ALL(C)); ll ans=0,now=1; rep(i,n){ int j=C[i].second; ans=(ans+A[j]*now%MOD)%MOD; now=now*B[j]%MOD; } cout<