#B3916. Maximum Modular Polynomial Value

    ID: 11573 Type: Default 1000ms 256MiB

Maximum Modular Polynomial Value

Maximum Modular Polynomial Value

You are given nine integers A, B, C, D, E, F, G, P and four integers X1, X2, Y1, Y2. Consider the function

[ f(x, y) = \bigl(Ax^3 + By^3 + Cx^2y + Dxy^2 + Exy + Fx + Gy\bmod P \bigr), ]

where for any integer x, x \bmod K denotes the remainder when x is divided by K (for example, \(7 \bmod 3 = 1\)).

Your task is to find the maximum value of f(x, y) over all pairs of integers \(x, y\) such that:

[ X_{1} \le x \le X_{2}, \quad Y_{1} \le y \le Y_{2} ]

Note that the operations inside the parentheses are performed before taking the modulo with P.

inputFormat

The input consists of a single line containing 12 space-separated integers:

A B C D E F G P X1 X2 Y1 Y2

It is guaranteed that X1 ≤ X2 and Y1 ≤ Y2.

outputFormat

Output a single integer, which is the maximum value of the function f(x, y) over all valid pairs \(x, y\).

sample

1 1 1 1 1 1 1 10 0 2 0 2
7