Limited Memory

Generate a huge pseudorandom array with a linear recurrence and answer many order-statistic queries without storing the array.

Medium7Binary searchMathImplementationSortingNo attempts yetTime limit7sMemory limit4 MB

Problem

An array XX of NN integers is defined, but its elements are not given directly. They are generated by the rule below.

Each query is a single integer qq and asks for the qq-th smallest element of XX. The index qq is 0-based, so q=0q=0 asks for the smallest element and q=1q=1 for the second smallest. A value that appears several times is counted once per appearance.

This looks easy, because sorting the array and reading off the answers would settle it. The memory limit is too small for that: you cannot hold all of XX at once. The number of queries is small, so all of the query data does fit.

You are given NN, x0x_0, aa, bb and the list of queries. The pseudocode that builds XX is:

X[0] = x0
for i = 1 to N-1:
    X[i] = (X[i-1] * a + b) % 1000000007

The intermediate product exceeds 32 bits, so watch out for integer overflow.

Write a program that prints the sum of the answers to all queries.

Input

The first line contains the integers NN, x0x_0, aa and bb, separated by spaces. (1N2×1061 \le N \le 2 \times 10^6, 0x0,a,b109+60 \le x_0, a, b \le 10^9 + 6)

The second line contains the number of queries QQ. (1Q10001 \le Q \le 1000)

The third line contains QQ integers, the queries, separated by spaces. (0qN10 \le q \le N-1)

Output

Print the sum of the answers to all queries on one line. The value is smaller than 101310^{13}, so it fits in a 64-bit integer.