eolymp
bolt
Try our new interface for solving problems
Problems

Horses

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 xi. If you started year with h horses, you ended the year with h * xi horses in your herd.

Horses could only be sold at the end of a year. For each year i, Mansur remembers a positive integer yi: 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 yi.

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 xi or one of the values yi. 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 xi or yi 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 109 + 7.

Example

Suppose that there are n = 3 years, with the following information:

prb7769.gif

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 * X0 = 2 horses.
  • After year 1 he will have 2 * X1 = 2 horses. He can now sell those two horses. The total profit will be 2 * Y1 = 8.

Then, suppose that there is m = 1 update: changing y1 to 2. After the update we will have:

prb7769_1.gif

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 * X0 = 2 horses. He can now sell one of those horses for 1 * Y0 = 3, and have one horse left.
  • After year 1 he will have 1 * X1 = 1 horse.
  • After year 2 he will have 1 * X2 = 3 horses. He can now sell those three horses for 3 * Y2 = 3. The total amount of money is 3 + 3 = 6.

Input

The first line contains the number of years n (1n500000). Second line gives the growth coefficients x0, x1, ... xn-1 at the end of the i-th year (0in - 1), and third line gives the prices y0, y1, ... yn-1 of one horse at the end of i-th year (0in - 1). The next line gives the number of updates m (1m105). Each of the next m lines gives three integers type pos val (type = 1 for UpdateX, type = 2 for UpdateY, 0posn - 1). All the initial, as well as updated values of X and Y arrays are between 1 and 109 inclusive.

Output

Print in the first line the maximal amount of money (modulo 109 + 7) that Mansur could get for these initial values of X and Y. Then for each update print the maximal amount of money (modulo 109 + 7) that Mansur could get after this update.

Time limit 1 second
Memory limit 128 MiB
Input example #1
3
2 1 3
3 4 1
1
2 1 2
Output example #1
8
6
Source 2015 IOI, Day 2