N = int(input()) A = list(map(int,input().split())) B = list(map(int,input().split())) mod = 10**9+7 C = [] for i in range(N): tmp = (B[i]-1)/A[i] C.append([tmp,i]) C.sort() C.reverse() level = 1 ans = 0 for i,ii in C: ans+=level*A[ii] ans = ans%mod level*=B[ii] print(ans)