n = int(input()) a = list(map(int,input().split())) b = list(map(int,input().split())) def compare(ab1,ab2): a1,b1 = ab1 a2,b2 = ab2 if a1 + b1 * a2 >= a2 + a1 * b2:return -1 else:return 1 ab = [tmp for tmp in zip(*[a,b])] from functools import cmp_to_key ab.sort(key=cmp_to_key(compare)) ans = 0 lb = 1 mod = 10 ** 9 + 7 for aa,bb in ab: ans += lb * aa lb *= bb ans %= mod lb %= mod print(ans)