n = int(input()) A = list(map(int,input().split())) B = list(map(int,input().split())) MOD = 10**9+7 job_weight = [] for i in range(n): job_weight.append(((B[i]-1)/A[i],i)) job_weight.sort(reverse=True) money = 0 job_power = 1 for _,job in job_weight: money += A[job]*job_power money %= MOD job_power *= B[job] job_power %= MOD print(money)