Goodeat has a white chessboard with n rows and m columns.
Each grid (i, j) has a weight c(i, j). At any time, the grid (i, j) can be dyed black at the cost of c(i, j).
Goodeat has a special talent. For the four intersecting squares of any two rows and two columns, if three of them are black squares, Goodeat can dye the fourth square black without any cost.
Please find out the minimum cost of dyeing a black chessboard.
Due to the large number of grids, we use the following method to generate weights:
A0 = a
A(i+1) = (Ai * Ai * b + Ai * c + d)% p
Where A(m*(i-1)+j) is the cost c(i, j) of the grid in the i-th row and j-th column
)
.
输入描述:
The first line contains seven integers n,m,a,b,c,d,p.(
)
输出描述:
Output a single integer denoting the answer.
示例1
说明
Here is the weight matrix:
2 4 6 3
3 3 3 3
3 3 3 3
3 3 3 3
备注: