Snuke has decided to play with cards and a deque (that is, a double-ended queue). Each card shows an integer from through , and the deque is initially empty.
Snuke will insert the cards at the beginning or the end of the deque one at a time, in order from to . Then, he will perform the following action times: take out the card from the beginning or the end of the deque and eat it.
Afterwards, we will construct an integer sequence by arranging the integers written on the eaten cards, in the order they are eaten. Among the sequences that can be obtained in this way, find the number of the sequences such that the element is . Print the answer modulo .
The input is given from Standard Input in the following format:
Print the answer modulo .
Birzhan has cards, numbered from to . Every card has each number from to written on it. Some numbers exist on the front side of the card and some on the back side. No number exists on both the sides of a card at the same time. These cards are placed on the table in a row such that only one side is visible. Birzhan is allowed to flip them any number of times.
Now, Birzhan has to answer queries each of them consisting of two integers and (). We define as the sum of the squares of every integer from to if it exists on the visible side of any card numbered from to . Given that Birzhan can flip any number of cards any number of times, find and print the maximum value of .
For example, given cards and we have the as follows:-
The first line contains three space-separated integers, , , and , denoting the number of cards, what numbers written on each card, and the number of queries, respectively.
Each of the next lines describes the cards. On each line, the first number is , denoting the count of numbers written on the visible side of the card. Next space-separated unique integers represent the numbers written on the visible side of that card, each between and .
Next lines contain two space-separated integers, and , describing the query mentioned above.
Print the maximum value of for each query.
As it has been found out recently, all the Berland’s current economical state can be described using a simple table in size. ― the number of days in each Berland month, ― the number of months. Thus, a table cell corresponds to a day and a month of the Berland’s year. Each cell will contain either , or , which means the state’s gains in a particular month, on a particular day. corresponds to profits, corresponds to losses. It turned out important for successful development to analyze the data on the state of the economy of the previous year, however when the treasurers referred to the archives to retrieve the data, it turned out that the table had been substantially damaged. In some table cells the number values had faded and were impossible to be deciphered. It is known that the number of cells in which the data had been preserved is strictly less than . However, there is additional information ― the product of the numbers in each line and column equaled . Your task is to find out how many different tables may conform to the preserved data. As the answer to the task can be quite large, you have to find it modulo .
The first line contains integers and . The second line contains the integer ― the number of cells in which the data had been preserved. The next lines contain the data on the state of the table in the preserved cells. Each line is of the form
a b c, where ― the number of the table row, ― the number of the column, ― the value containing in the cell ( or ). They are numbered starting from . It is guaranteed that no two lines with same and values exist. The last line contains an integer .
Print the number of different tables that could conform to the preserved data modulo .