#include "horses.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MOD = 1e9+7;
struct segtree {
#define lc 2*v
#define rc 2*v+1
#define tm (tl+tr)/2
struct node {
long double logsum = 0;
long double logprof = 0;
ll product = 1;
ll profit = 0;
};
vector<node> tree;
int n;
vector<int> x, y;
void pushup(int v) {
tree[v].logsum =
tree[lc].logsum+tree[rc].logsum;
tree[v].product = (tree[lc].product*tree[rc].product) % MOD;
long double a = tree[lc].logsum+tree[rc].logprof;
long double b = tree[lc].logprof;
if (a > b) {
tree[v].logprof = a;
tree[v].profit = (tree[lc].product*tree[rc].profit) % MOD;
}
else {
tree[v].logprof = b;
tree[v].profit = tree[lc].profit;
}
}
void build(int v, int tl, int tr) {
if (v == 1) {
tree.resize(4*n);
}
if (tl == tr) {
tree[v].logsum = log2((long double)x[tl]);
tree[v].logprof = tree[v].logsum+log2((long double)y[tl]);
tree[v].product = x[tl];
tree[v].profit = (x[tl]*y[tl]) % MOD;
return;
}
build(lc, tl, tm);
build(rc, tm+1, tr);
pushup(v);
}
void update(int v, int tl, int tr, int pos, int type, int val) {
if (pos < tl || pos > tr) return;
if (tl == tr) {
if (type == 1) // change the value of x
x[tl] = val;
else
y[tl] = val;
tree[v].logsum = log2((long double)x[tl]);
tree[v].logprof = tree[v].logsum+log2((long double)y[tl]);
tree[v].product = x[tl];
tree[v].profit = (x[tl]*y[tl]) % MOD;
return;
}
update(lc, tl, tm, pos, type, val);
update(rc, tm+1, tr, pos, type, val);
pushup(v);
}
int query() {
return tree[1].profit;
}
};
segtree stree;
int init(int N, int X[], int Y[]) {
vector<int> x(N+1), y(N+1);
for (int i = 0; i < N; i++) {
x[i+1] = X[i];
y[i+1] = Y[i];
}
stree.n = N;
stree.x = x;
stree.y = y;
stree.build(1, 1, N);
return stree.query();
}
int updateX(int pos, int val) {
stree.update(1, 1, stree.n, pos+1, 1, val);
return stree.query();
}
int updateY(int pos, int val) {
stree.update(1, 1, stree.n, pos+1, 2, val);
return stree.query();
}