#int(input()) #map(int, input().split()) #list(map(int, input().split())) N = int(input()) A = list(map(int, input().split())) B = list(map(int, input().split())) mod = 10 ** 9 + 7 c = [0] * N for i in range(N): if B[i] == 1: u = 10 ** 10 else: u = A[i] / (B[i]-1) c[i] = [A[i], B[i], u] c = sorted(c, key=lambda x:(x[2])) ans = 0 x = 1 # u = [1, 0, 5, 7, 3, 4, 2, 6] for i in range(N): # for i in u: # print(c[i]) ans += x * c[i][0] ans %= mod x = (x * c[i][1]) % mod print(ans)