#include const int Mod = 1000000007; typedef struct { int key[2], 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] = ((long long)x[p].key[0] * x[q].key[1] <= (long long)x[q].key[0] * x[p].key[1])? 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[0] = A[i]; d[i-1].key[1] = 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; }