N = int(input()) A = list(map(int, input().split())) B = list(map(int, input().split())) C = [] for i, (a, b) in enumerate(zip(A, B)): C.append([(b-1)/a,i]) C.sort(reverse=True) res = 0 lev = 1 mod =10**9+7 for i in range(N): res += A[C[i][1]]*lev res %= mod lev *= B[C[i][1]] lev %= mod print(res)