#include<cstdio> #include<algorithm> usingnamespace std; template <classType> voidread(Type &x) { char c; bool flag = false; while ((c = getchar()) < '0' || c > '9') c == '-' && (flag = true); x = c - '0'; while ((c = getchar()) >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0'; if (flag) x = ~x + 1; } template <classType, class ...rest> voidread(Type &x, rest &...y){ read(x), read(y...); } template <classType> voidwrite(Type x) { if (x < 0) putchar('-'), x = ~x + 1; if (x > 9) write(x / 10); putchar(x % 10 + '0'); } typedeflonglong LL; constint N = 5e5 + 10, mod = 1e9 + 7; int n, A[N], B[N]; intmain() { read(n); for (int i = 1; i <= n; ++i) read(A[i]); for (int i = 1; i <= n; ++i) read(B[i]); LL cur = 1; for (int i = 1; i <= n; ++i) if (A[i] == 1) { cur += B[i]; swap(A[i], A[n]), swap(B[i], B[n]); --i, --n; } int t = -1; for (int i = 1; i <= n; ++i) if (!~t || ((cur + B[i]) * A[t] > (cur + B[t]) * A[i])) t = i; if ((LL)(A[t] - 1) * cur > B[t]) t = 0; else cur += B[t]; for (int i = 1; i <= n; ++i) if (i ^ t) cur = cur * A[i] % mod; write(cur); return0; }