N=int(input()) A=list(map(int,input().split())) B=list(map(int,input().split())) from decimal import * getcontext().prec=50 X=[(Decimal(B[i]-1)/Decimal(A[i]),A[i],B[i]) for i in range(N)] X.sort(reverse=True) ANS=0 V=1 mod=998244353 mod=10**9+7 for i in range(N): ANS=(ANS+V*X[i][1])%mod V=(V*X[i][2])%mod print(ANS)