codeforces#P1801E. Gasoline prices
Gasoline prices
Description
There is one gas station in each city of Berland. Gas stations have special pricing, and for each gas station there is a fixed range of prices for which they are ready to sell gasoline. A gas station in the city with the number is ready to sell gasoline at any price from to inclusive.
The King of Berland — is an exemplary family man, and for years, two sons were born to him every year. The king's children have been involved in public affairs since early childhood, and at the end of each year they check the honesty of gasoline prices. From birth, the king's children, who are born in the year , are responsible for checking gasoline prices on the ways from the city of to the city of and from the city of to the city of , respectively.
The check is as follows: both children simultaneously start their journey from the cities and , respectively. The first son of the king, born in the year , moves along the path from the city to the city , and the second — from the city to the city . Children check that the price of gasoline in the city of coincides with the price of gasoline in the city of . Next, they check that the price of gasoline in the second city on the way from to coincides with the price in the second city on the way from to . Then they repeat the same thing for a couple of third cities on their paths and so on. At the end, they check that the price of gasoline in the city of coincides with the price of gasoline in the city of . It is guaranteed that the length of the path from the city to the city coincides with the length of the path from the city to the city .
Gas stations must strictly obey the laws, and therefore all checks of gasoline prices should not reveal violations. Help Berland gas stations find out how many ways they can set gasoline prices for years. In other words, for each from to , calculate how many ways you can set gasoline prices at all gas stations so that after the birth of the first pairs of the king's children, all their checks did not reveal violations, and at any gas station the price was in the acceptable price range. Since the number of such methods can be large, calculate the answer modulo .
The first line contains a single integer () — the number of cities in Berland.
The second line contains numbers (), where denotes the number of the next city on the way from city to city .
In each of the following lines, two integers are given and (), specifying the acceptable range of prices at the gas station number .
The following line contains a single integer () — the number of years during which two sons were born to the king.
In each of the following lines, four integers are given , , and (), specifying two paths on which the king's children will check gasoline prices, born in the year . It is guaranteed that the length of the path between the cities and is equal to the length of the path between the cities and .
In lines, print one number each. The number in the line should be equal to the number of ways to set gasoline prices in all cities so that the king's children born in the years up to and including do not reveal violations in inspections. Output the numbers modulo .
Input
The first line contains a single integer () — the number of cities in Berland.
The second line contains numbers (), where denotes the number of the next city on the way from city to city .
In each of the following lines, two integers are given and (), specifying the acceptable range of prices at the gas station number .
The following line contains a single integer () — the number of years during which two sons were born to the king.
In each of the following lines, four integers are given , , and (), specifying two paths on which the king's children will check gasoline prices, born in the year . It is guaranteed that the length of the path between the cities and is equal to the length of the path between the cities and .
Output
In lines, print one number each. The number in the line should be equal to the number of ways to set gasoline prices in all cities so that the king's children born in the years up to and including do not reveal violations in inspections. Output the numbers modulo .
Note
Consider the first example.
After the birth of the first two sons, the prices in the cities of and should be equal. In total, there are 2 ways to choose the same gasoline price for the cities of and so that it falls within the acceptable price range for these cities. So, there are only ways to set gasoline prices: .
The second pair of sons will check the prices on the paths and . This means that gasoline prices in the cities of and must match, which is already being done. Therefore, after the birth of the second pair of sons, the answer did not change in any way.
The third pair of sons will check the prices on the tracks and . Then the price of non-gasoline in the city of should be equal to the price in the city of , and the price in the city of should be equal to the price in the city of . Prices in the cities of and are already the same. For the cities of and , there are 2 ways to choose the same price for gasoline so that it falls within the acceptable price range for these cities. So, there are only ways to set gasoline prices: . The fourth pair of sons will check the prices on the tracks and . This means that the prices in the cities of and should be equal, and since the prices in the cities of and already coincide, then in the cities of , and there should be the same price for gasoline. The price of gasoline in the city of should be no more than 3, and the price of gasoline in the city of should be no less than 4. So, after the birth of the fourth pair of sons, there are no ways to set gasoline prices so that all checks are carried out and prices are in the required ranges.