Black and white
题号:NC224093
时间限制:C/C++/Rust/Pascal 2秒,其他语言4秒
空间限制:C/C++/Rust/Pascal 512 M,其他语言1024 M
64bit IO Format: %lld

题目描述

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

输入

复制
4 4 1 2 3 4 7

输出

复制
20

说明

Here is the weight matrix:
2 4 6 3 
3 3 3 3 
3 3 3 3 
3 3 3 3 

备注: