N = int(input()) A = list(map(int,input().split())) B = list(map(int,input().split())) P = 10 ** 9 + 7 inf = 10 ** 9 def f(x): a,b = x if b == 1: return inf else: return a / (b - 1) C = [(a,b) for a,b in zip(A,B)] C.sort(key = f) ans = 0 now = 1 for a,b in C: ans += a * now ans %= P now = now * b % P print(ans)