Codeforces Beta Round 39
E
数字表
Number Table
Codeforces
2500
原题
← D→

数字表AI (CI翻译)

As 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*$.

最近人们发现,别兰的当前经济状况可以用一个大小为 $*n* × *m*$ 的简单表格来描述。$*n*$ 表示别兰每个月的天数,$*m*$ 表示月份数。因此,表格中的每个单元格都对应别兰一年中的某个月份和某一天。每个单元格中都包含 1 或 -1,表示国家在某个月某一天的收益情况:1 表示盈利,-1 表示亏损。事实证明,分析上一年的经济数据对经济的成功发展至关重要。然而,当财务官员查阅档案以获取数据时,发现表格已经严重损坏,部分单元格中的数值已经褪色,无法辨认。已知数据得以保留的单元格数量严格小于 $*max*(*n*, *m*)$。不过,还有一条额外信息:每一行和每一列中所有数的乘积都等于 -1。你的任务是求有多少种不同的表格可能符合已保留的数据。由于答案可能很大,请将结果对 $*p*$ 取模。AI (CI翻译)

Input

输入格式

AI (CI翻译)

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$).

第一行包含两个整数 $*n*$ 和 $*m*$ ($1 ≤ *n*, *m* ≤ 1000$)。第二行包含整数 $*k*$ ($0 ≤ *k* < *max*(*n*, *m*)$),表示数据得以保留的单元格数量。接下来 $*k*$ 行描述表格中已保留数据的状态。每行的格式为 "$*a*$ $*b*$ $*c*$",其中 $*a*$ ($1 ≤ *a* ≤ *n*$) 表示表格的行号,$*b*$ ($1 ≤ *b* ≤ *m*$) 表示列号,$*c*$ 表示该单元格中的值(1 或 -1)。行和列均从 $1$ 开始编号。保证不存在两行具有相同的 $*a*$ 和 $*b*$ 值。最后一行包含整数 $*p*$ ($2 ≤ *p* ≤ 10^{9} + 7$)。AI (CI翻译)

Output

输出格式

AI (CI翻译)

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

输出可能符合已保留数据的不同表格数量对 $*p*$ 取模后的结果。AI (CI翻译)

样例

Input #1
2 2
0
100
Output #1
2
InputCopy OutputCopy
Input #2
2 2
1
1 1 -1
100
Output #2
1
InputCopy OutputCopy
题目信息
题号
40E
时间限制
2 s
内存限制
216 MB
标签
组合数学

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。