N = int(input()) A = list(map(int,input().split())) B = list(map(int,input().split())) MOD = 10**9+7 AB = [(a,b) for a,b in zip(A,B)] INF = float('inf') AB.sort(key=lambda x:INF if x[1] == 1 else x[0]/(x[1]-1)) l = 1 ans = 0 for a,b in AB: ans += a*l ans %= MOD l *= b l %= MOD print(ans)