mod = 1000000007


def main():
    import sys
    input = sys.stdin.readline
    from functools import cmp_to_key

    def cmp_func(X, Y):
        xy0 = X[0] + Y[0] * X[1]
        xy1 = Y[0] + X[0] * Y[1]
        if xy0 == xy1:
            return 0
        elif xy0 > xy1:
            return -1
        else:
            return 1

    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))

    AB = [(a, b) for a, b in zip(A, B)]
    AB.sort(key=cmp_to_key(cmp_func))

    ans = 0
    x = 1
    for a, b in AB:
        ans = (ans + (a * x) % mod)%mod
        x = (x * b)%mod
    print(ans % mod)


if __name__ == '__main__':
    main()