## Array Distances

View as PDF

Points: 7 (partial)
Time limit: 2.0s
Memory limit: 64M

Author:
Problem type

You have been given an array of unique integers (with starting index ) and queries to perform. For each query, you are given an index . For each integer in the array, you must calculate the product of the integer and the distance between their index and the given index . That is, if the integer has index you must calculate . Output, for each query, the sum of the calculations for all integers in the array, modulo .

#### Input Specification

The first line will contain two integers, and , separated by a space. The second line will contain space-separated unique integers, . The next lines will contain one integer, an index .

#### Output Specification

Output, on separate lines, the answer to each query, modulo .

#### Sample Input 1

9 5
10 6 4 5 6 4 11 9 3
8
3
7
5
2

#### Sample Output 1

186
162
152
136
188

#### Sample Input 2

9 3
4 5 4 11 11 10 7 9 6
4
2
8

#### Sample Output 2

144
234
188

#### Explanation for Sample Input 1

For the first query, . The calculations then become: