This page is still under construction.

Parts of this page are still being built. What you see may change.

Limited Memory

Time limit7sMemory limit4 MB

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

Medium7 of 10

Topics
Binary search, Math, Implementation, Sorting
Solved
No attempts yet

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. (1≤N≤2×1061 \le N \le 2 \times 10^6, 0≤x0,a,b≤109+60 \le x_0, a, b \le 10^9 + 6)

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

The third line contains QQ integers, the queries, separated by spaces. (0≤q≤N−10 \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.

Examples3

  1. Example 1

    Input
    5 100 1 5
    2
    0 3
    
    Expected output
    215
    
  2. Example 2

    Input
    1 0 0 0
    1
    0
    
    Expected output
    0
    
  3. Example 3

    Input
    5 7 0 3
    3
    0 2 4
    
    Expected output
    13