Horses
Mansur loves to breed horses, just like his ancient ancestors did. He now has the largest herd in Kazakhstan. But this was not always the case. n years ago, Mansur was just a dzhigit (Kazakh for a young man) and he only had a single horse. He dreamed to make a lot of money and to finally become a bai (Kazakh for a very rich person).
Let us number the years from 0 to n - 1 in chronological order (i.e., year n - 1 is the most recent one). The weather of each year influenced the growth of the herd. For each year i, Mansur remembers a positive integer growth coefficient x[i]
. If you started year with h horses, you ended the year with h * x[i]
horses in your herd.
Horses could only be sold at the end of a year. For each year i, Mansur remembers a positive integer y[i]
: the price for which he could sell a horse at the end of year i. After each year, it was possible to sell arbitrarily many horses, each at the same price y[i]
.
Mansur wonders what is the largest amount of money he could have now if he had chosen the best moments to sell his horses during the n years. You have the honor of being a guest on Mansur’s toi (Kazakh for holiday), and he asked you to answer this question.
Mansur’s memory improves throughout the evening, and so he makes a sequence of m updates. Each update will change either one of the values x[i]
or one of the values y[i]
. After each update he again asks you the largest amount of money he could have earned by selling his horses. Mansur’s updates are cumulative: each of your answers should take into account all of the previous updates. Note that a single x[i]
or y[i]
may be updated multiple times.
The actual answers to Mansur’s questions can be huge. In order to avoid working with large numbers, you are only required to report the answers modulo 10^9
+ 7.
Example
Suppose that there are n = 3 years, with the following information:
For these initial values, Mansur can earn the most if he sells both his horses at the end of year 1. The entire process will look as follows:
Initially, Mansur has 1 horse.
After year 0 he will have 1 *
X[0]
= 2 horses.After year 1 he will have 2 *
X[1]
= 2 horses. He can now sell those two horses. The total profit will be 2 *Y[1]
= 8.
Then, suppose that there is m = 1 update: changing y[1]
to 2. After the update we will have:
In this case, one of the optimal solutions is to sell one horse after year 0 and then three horses after year 2. The entire process will look as follows:
Initially, Mansur has 1 horse.
After year 0 he will have 1 *
X[0]
= 2 horses. He can now sell one of those horses for 1 *Y[0]
= 3, and have one horse left.After year 1 he will have 1 *
X[1]
= 1 horse.After year 2 he will have 1 *
X[2]
= 3 horses. He can now sell those three horses for 3 *Y[2]
= 3. The total amount of money is 3 + 3 = 6.
Input
The first line contains the number of years n (1 ≤ n ≤ 500000). Second line gives the growth coefficients x[0]
, x[1]
, ... x[n-1]
at the end of the i-th year (0 ≤ i ≤ n - 1), and third line gives the prices y[0]
, y[1]
, ... y[n-1]
of one horse at the end of i-th year (0 ≤ i ≤ n - 1). The next line gives the number of updates m (1 ≤ m ≤ 10^5
). Each of the next m lines gives three integers type pos val (type = 1 for UpdateX, type = 2 for UpdateY, 0 ≤ pos ≤ n - 1). All the initial, as well as updated values of X and Y arrays are between 1 and 10^9
inclusive.
Output
Print in the first line the maximal amount of money (modulo 10^9
+ 7) that Mansur could get for theseinitial values of X and Y. Then for each update print the maximal amount of money (modulo 10^9
+ 7) that Mansur could get after this update.