inf = 10 ** 18 def merge(A, left, mid, right): mx = (inf, 0) L = [0 for i in range(0, mid - left + 1)] R = [0 for j in range(0, right - mid + 1)] L[mid - left] = mx R[right - mid] = mx for i in range(0, mid - left): L[i] = A[left + i] for j in range(0, right - mid): R[j] = A[mid + j] i = 0 j = 0 cnt = 0 for k in range(left, right): Lx, Ly = L[i] Rx, Ry = R[j] if L[i] == mx: A[k] = R[j] j = j + 1 elif R[j] == mx: A[k] = L[i] i = i + 1 elif Lx == 1: A[k] = R[j] j = j + 1 elif Rx == 1: A[k] = L[i] i = i + 1 else: if Rx * (Ly - 1) > Ry * (Lx - 1): A[k] = L[i] i = i + 1 else: A[k] = R[j] j = j + 1 cnt = cnt + 1 return A def mergeSort(A, left, right): if left + 1 < right: mid = (left + right) // 2 mergeSort(A, left, mid) mergeSort(A, mid, right) cnt = merge(A, left, mid, right) return cnt N = int(input()) A = list(map(int, input().split())) B = list(map(int, input().split())) mod = 10 ** 9 + 7 D = [] for i in range(N): D.append((A[i], B[i])) mergeSort(D, 0, N) ans = 0 level = 1 for i in range(N): A, B = D[i] ans += level * A level *= B ans %= mod level %= mod print(ans)