Flowers and Chocolate
Contest: MemSQL Start[c]UP 3.0 - Round 2 (onsite finalists)
Difficulty rating: 3300
Tags: combinatorics, math, matrices

It's Piegirl's birthday soon, and Pieguy has decided to buy her a bouquet of flowers and a basket of chocolates.

The flower shop has F different types of flowers available. The i-th type of flower always has exactly pi petals. Pieguy has decided to buy a bouquet consisting of exactly N flowers. He may buy the same type of flower multiple times. The N flowers are then arranged into a bouquet. The position of the flowers within a bouquet matters. You can think of a bouquet as an ordered list of flower types.

The chocolate shop sells chocolates in boxes. There are B different types of boxes available. The i-th type of box contains ci pieces of chocolate. Pieguy can buy any number of boxes, and can buy the same type of box multiple times. He will then place these boxes into a basket. The position of the boxes within the basket matters. You can think of the basket as an ordered list of box types.

Pieguy knows that Piegirl likes to pluck a petal from a flower before eating each piece of chocolate. He would like to ensure that she eats the last piece of chocolate from the last box just after plucking the last petal from the last flower. That is, the total number of petals on all the flowers in the bouquet should equal the total number of pieces of chocolate in all the boxes in the basket.

How many different bouquet+basket combinations can Pieguy buy? The answer may be very large, so compute it modulo 1000000007 = 109 + 7.

Input format:
The first line of input will contain integers F, B, and N (1 ≤ F ≤ 10, 1 ≤ B ≤ 100, 1 ≤ N ≤ 1018), the number of types of flowers, the number of types of boxes, and the number of flowers that must go into the bouquet, respectively.

The second line of input will contain F integers p1, p2, ..., pF (1 ≤ pi ≤ 109), the numbers of petals on each of the flower types.

The third line of input will contain B integers c1, c2, ..., cB (1 ≤ ci ≤ 250), the number of pieces of chocolate in each of the box types.

Output format:
Print the number of bouquet+basket combinations Pieguy can buy, modulo 1000000007 = 109 + 7.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
In the first example, there is 1 way to make a bouquet with 9 petals (3 + 3 + 3), and 1 way to make a basket with 9 pieces of chocolate (3 + 3 + 3), for 1 possible combination. There are 3 ways to make a bouquet with 13 petals (3 + 5 + 5, 5 + 3 + 5, 5 + 5 + 3), and 5 ways to make a basket with 13 pieces of chocolate (3 + 10, 10 + 3, 3 + 3 + 7, 3 + 7 + 3, 7 + 3 + 3), for 15 more combinations. Finally there is 1 way to make a bouquet with 15 petals (5 + 5 + 5) and 1 way to make a basket with 15 pieces of chocolate (3 + 3 + 3 + 3 + 3), for 1 more combination.

Note that it is possible for multiple types of flowers to have the same number of petals. Such types are still considered different. Similarly different types of boxes may contain the same number of pieces of chocolate, but are still considered different.

Example input:
2 3 3
3 5
10 3 7

Example output:
17

Example input:
6 5 10
9 3 3 4 9 9
9 9 1 6 4

Example output:
31415926

Editorial:
Let's first consider how to compute the number of ways to make a bouquet with exactly K petals. Define a polynomial $$P(x) = \sum_{i=1}^{F} x^{p_i}$$. Then if we compute P(x)N, the coefficient of xK gives the number of ways to make a bouquet with exactly K petals. This is because each possible bouquet produces a term with an exponent equal to its number of petals.
Now lets consider how to compute the number of ways to make a basket with exactly K chocolates. Define a polynomial $$Q(x) = 1 - \sum_{i=1}^{B} x^{c_i}$$. Then if we compute $$x^{-K} \pmod{Q(x)}$$, the coefficient of x0 gives the number of ways to make a basket with exactly K chocolates. This can be derived from a Generating Function, but we will provide an alternate derivation.
Consider the following algorithm for computing the number of ways to make a basket with K chocolates:
The final answer is the sum, over all values of K, of the number of bouquets with K petals times the number of baskets with K chocolates. The number of bouquets is given by the xK coefficient of P(x)N, or equivalently, the x - K coefficient of P(x - 1)N, and the number of baskets is given by the coefficient of x0 of $$x^{-K} \pmod{Q(x)}$$. It follows that the answer is simply the coefficient of x0 of $$P(x^{-1})^N \pmod{Q(x)}$$. This can be computed using $${\mathcal O}\left(\log N+\sum\log p_{i}\right)$$ polynomial multiplications, each of which takes O(max(ci)2) time, using naive multiplication, for a total runtime of $${\cal O}\left(\left(\log N+\sum \log p_{i}\right)\cdot max(c_{i})^{2}\right)$$.