n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) mod = 10**9+7 rab = [] for ai, bi in zip(a, b): rab.append(((bi-1)/ai, ai, bi)) rab.sort() x = 1 ans = 0 for _, ai, bi in rab[::-1]: ans = (ans+ai*x)%mod x = bi*x%mod print(ans)