import sys #input = sys.stdin.readline #input = sys.stdin.buffer.readline #文字列はダメ #sys.setrecursionlimit(1000000) import math import bisect #import itertools #import random from heapq import heapify, heappop, heappush from collections import defaultdict #from collections import deque import copy #DeepCopy: hoge = [_[:] for _ in hogehoge] #from functools import lru_cache #@lru_cache(maxsize=None) MOD = pow(10,9) + 7 #MOD = 998244353 #dx = [1,0,-1,0] #dy = [0,1,0,-1] #dx8 = [1,1,0,-1,-1,-1,0,1] #dy8 = [0,1,1,1,0,-1,-1,-1] from functools import cmp_to_key def compare(x,y): #aが大きいとき1 a1,b1 = x a2,b2 = y p = a1 + a2*b1 q = a2 + a1*b2 if p == q: return 0 if p < q: return 1 if p > q: return -1 def main(): N = int(input()) A = list(map(int,input().split())) B = list(map(int,input().split())) C = [] for a,b in zip(A,B): C.append((a,b)) C.sort(key = cmp_to_key(compare)) # print(C) level = 1 ans = 0 for a,b in C: ans += level*a ans %= MOD level *= b level %= MOD print(ans) if __name__ == '__main__': main()