#include using namespace std; #ifdef _RUTHEN #include "debug.hpp" #else #define show(...) true #endif using ll = long long; #define rep(i, n) for (int i = 0; i < (n); i++) template using V = vector; #include using mint = atcoder::modint1000000007; int main() { ios::sync_with_stdio(false); cin.tie(0); int N; cin >> N; V> AB(N); rep(i, N) cin >> AB[i].first; rep(i, N) cin >> AB[i].second; sort(AB.begin(), AB.end(), [](pair x, pair y) { return x.first + x.second * y.first > y.first + y.second * x.first; }); show(AB); mint ans = 0, cur = 1; rep(i, N) { ans += cur * AB[i].first; cur *= AB[i].second; } cout << ans.val() << '\n'; return 0; }