MOD = 10 ** 9 + 7 N = gets.to_i A = gets.split.map(&:to_i) B = gets.split.map(&:to_i) a_sum = A.sum b_sum = B.sum C = A.zip(B) C.sort_by! { |a, b| Rational(b - 1, a) } C.reverse! ans = 0 level = 1 C.each do |a, b| ans += level * a ans %= MOD level *= b end puts ans