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. 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 to
in chronological order (i.e., year
is the most recent one). The weather of each year influenced the growth of the herd. For each year
, Mansur remembers a positive integer growth coefficient
. If you started year
with
horses, you ended the year with
horses in your herd.
Horses could only be sold at the end of a year. For each year , Mansur remembers a positive integer
: the price for which he could sell a horse at the end of year
. After each year, it was possible to sell arbitrarily many horses, each at the same price
.
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 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 updates. Each update will change either one of the values
or one of the values
. 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
or
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 .
Example
Suppose that there are years, with the following information:
0 | 1 | 2 | |
---|---|---|---|
X | 2 | 1 | 3 |
Y | 3 | 4 | 1 |
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
horses.
- After year 1 he will have
horses.
- He can now sell those two horses. The total profit will be
.
Then, suppose that there is update: changing
to
.
After the update we will have:
0 | 1 | 2 | |
---|---|---|---|
X | 2 | 1 | 3 |
Y | 3 | 2 | 1 |
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
horses.
- He can now sell one of those horses for
, and have one horse left.
- After year 1 he will have
horse.
- After year 2 he will have
horses.
- He can now sell those three horses for
. The total amount of money is
.
Your task is, given ,
,
and the list of updates, before the first update and after every update, compute the maximal amount of money that Mansur could get for his horses, modulo
.
You may assume that all the initial, as well as updated values of and
are between
and
inclusive.
Input Specification
Line of input will contain the single integer
representing the number of years.
Line of input will contain the array
, with adjacent numbers separated by a single space. For
,
gives the growth coefficient for year
. Note that this is the initial value given by Mansur (before any updates).
Line of input will contain the array
, with adjacent numbers separated by a single space. For
,
gives the price of a horse after year
. Note that this is the initial value given by Mansur (before any updates).
Line of input will contain the single integer
, representing the number of updates to follow.
Lines will each contain three space-separated numbers
,
, and
(
for updating
and
for updating
).
is an integer from the range
and
is the new value for
.
Output Specification
The first line of output should contain a single integer representing the maximal amount of money Mansur could get for the initial values of and
(before any of the
updates), modulo
.
For each of the next updates, output on a separate line containing a single integer representing the maximal amount of money Mansur could get after this update, modulo
.
Sample Input
3
2 1 3
3 4 1
1
2 1 2
Sample Output
8
6
Subtasks
subtask | points | additional constraints | ||
---|---|---|---|---|
1 | 17 | |||
2 | 17 | none | ||
3 | 20 | |||
4 | 23 | none | ||
5 | 23 | none |
Comments