#include const int Mod = 1000000007; double sup = 1e+12; typedef struct { double key; int id; } data; void merge_sort(int n, data x[]) { static data y[200001] = {}; if (n <= 1) return; merge_sort(n / 2, &(x[0])); merge_sort((n + 1) / 2, &(x[n/2])); int i, p, q; for (i = 0, p = 0, q = n / 2; i < n; i++) { if (p >= n / 2) y[i] = x[q++]; else if (q >= n) y[i] = x[p++]; else y[i] = (x[p].key < x[q].key)? x[p++]: x[q++]; } for (i = 0; i < n; i++) x[i] = y[i]; } int main() { int i, N, A[200001], B[200001]; scanf("%d", &N); for (i = 1; i <= N; i++) scanf("%d", &(A[i])); for (i = 1; i <= N; i++) scanf("%d", &(B[i])); data d[200001]; for (i = 1; i <= N; i++) { d[i-1].key = (B[i] == 1)? sup: (double)A[i] / (B[i] - 1); d[i-1].id = i; } merge_sort(N, d); long long ans = 0, tmp = 1; for (i = 0; i < N; i++) { ans += tmp * A[d[i].id]; tmp = tmp * B[d[i].id] % Mod; } printf("%lld\n", ans % Mod); fflush(stdout); return 0; }