n = int(input()) mod = 10**9+7 a = [int(i) for i in input().split()] b = [int(i) for i in input().split()] ab = [] for i in range(n): ab.append([a[i],b[i]]) li = [] #a1,b1 = ab[0] #li.append([0,0]) #one = [] for i in range(n): li.append([(ab[i][1]-1)/ab[i][0],i]) #print(li) li.sort(reverse=True) #print(li) #exit() #ab.sort(key=lambda x:(-x[1],x[0])) #print(ab) x = 1 ans = 0 for i in range(len(li)): #print(li[i][1]) ans += ab[li[i][1]][0]*x ans %= mod x *= ab[li[i][1]][1] x %= mod print(ans)