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