MOD = 10**9 + 7 N = int(input()) A = list(map(int, input().split())) B = list(map(int, input().split())) def key(x): a, b = x return (b - 1) / a xs = list(zip(A, B)) x = 1 ans = 0 for a, b in sorted(xs, key=key, reverse=True): ans += a * x ans %= MOD x *= b x %= MOD print(ans)