N = int(input()) A = list(map(int,input().split())) B = list(map(int,input().split())) C = [(a,b) for a,b in zip(A,B)] C.sort(key = lambda x:x[1],reverse = True) P = 10 ** 9 + 7 ans = 0 now = 1 for a,b in C: ans += now * a ans %= P now = now * b % P print(ans)