MOD = 10 ** 9 + 7 class f: def __init__(self, a, b): self.a = a self.b = b def __lt__(self, other): return (1 - self.b) * other.a < (1 - other.b) * self.a n = int(input()) A = list(map(int, input().split())) B = list(map(int, input().split())) ab = [f(a, b) for a, b in zip(A, B)] ab.sort() x = 1 ans = 0 for tmp in ab: a = tmp.a b = tmp.b ans += a * x ans %= MOD x *= b x %= MOD print(ans)