Package for this problem was not updated by the problem writer or Codeforces administration after we’ve upgraded the judging servers. To adjust the time limit constraint, solution execution time will be multiplied by 2. For example, if your solution works for 400 ms on judging servers, then value 800 ms will be displayed and used to determine the verdict.

Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ACM-ICPC mode for virtual contests.
If you've seen these problems, a virtual contest is not for you - solve these problems in the archive.
If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive.
Never use someone else's code, read the tutorials or communicate with other person during a virtual contest.

No tag edit access

E. Number Table

time limit per test

2 secondsmemory limit per test

216 megabytesinput

standard inputoutput

standard outputAs it has been found out recently, all the Berland's current economical state can be described using a simple table *n* × *m* in size. *n* — the number of days in each Berland month, *m* — 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 1, or -1, which means the state's gains in a particular month, on a particular day. 1 corresponds to profits, -1 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 *max*(*n*, *m*). However, there is additional information — the product of the numbers in each line and column equaled -1. 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 *p*.

Input

The first line contains integers *n* and *m* (1 ≤ *n*, *m* ≤ 1000). The second line contains the integer *k* (0 ≤ *k* < *max*(*n*, *m*)) — the number of cells in which the data had been preserved. The next *k* lines contain the data on the state of the table in the preserved cells. Each line is of the form "*a* *b* *c*", where *a* (1 ≤ *a* ≤ *n*) — the number of the table row, *b* (1 ≤ *b* ≤ *m*) — the number of the column, *c* — the value containing in the cell (1 or -1). They are numbered starting from 1. It is guaranteed that no two lines with same *a* and *b* values exist. The last line contains an integer *p* (2 ≤ *p* ≤ 10^{9} + 7).

Output

Print the number of different tables that could conform to the preserved data modulo *p*.

Examples

Input

2 2

0

100

Output

2

Input

2 2

1

1 1 -1

100

Output

1

Codeforces (c) Copyright 2010-2017 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: May/27/2017 18:35:43 (c2).

Desktop version, switch to mobile version.
User lists

Name |
---|