MOD = 10 ** 9 + 7 N = int(input()) A = list(map(int,input().split())) B = list(map(int,input().split())) AB = [] for i in range(N): a = A[i] b = B[i] AB.append(((b-1)/a,a,b)) AB.sort(key = lambda x:-x[0]) level = 1 ans = 0 for _,a,b in AB: ans += level * a ans %= MOD level *= b level %= MOD print(ans)