#P7626. [2018年杭电多校]Make ZYB Happy

[2018年杭电多校]Make ZYB Happy

Make ZYB Happy

Problem Description

It's known to all that ZYB is godlike, so obviously he has a large number of titles, such as jsking\texttt{jsking}, bijingzyb\texttt{bijingzyb} and nbazyb\texttt{nbazyb}. ZYB likes his titles very much. Each of ZYB's titles is a string consisting of lower case letters ’a’-’z’\texttt{'a'-'z'} associated with a happiness value hih_i, which shows how much ZYB likes this title. If you say any substring of some title with happiness value xx, he will get xx happiness points. Moreover, a string may appear in more than one title. In this case, the happiness points ZYB gets are multiplied. If the string you say is not the substring of any of his titles, he gets no happiness point. For example, let's say ZYB has two titles: zybnb\texttt{zybnb} (with happiness value 3) and ybyb\texttt{ybyb} (with happiness value 5). If you say y\texttt{y}, b\texttt{b} or yb\texttt{yb}, ZYB will get 15 happiness points; if you say z\texttt{z}, zy\texttt{zy} or zyb\texttt{zyb}, ZYB will only get 3 happiness points; if you say ybz\texttt{ybz} or ybac\texttt{ybac} he will get 0 happiness points. One day, you find ZYB pretty sad. As a big fan of ZYB, you want to say a word to ZYB to cheer him up. However, ZYB is really busy, so you can only say no more than mm letters. As you haven't seen ZYB for a long time, you are so excited that you forget what you want to say, so you decide to choose to say a nonempty string no longer than mm and only containing ’a’-’z’\texttt{'a'-'z'} with equal probability. You want to know the expectations of happiness points you will bring to ZYB for different mm.

Input

The first line contains an integer nn (1n104)(1 \leq n \leq 10^4), the number of titles ZYB has. The ii-th of the next nn lines contains a nonempty string tit_i, which only contains lower case letters ’a’-’z’\texttt{'a'-'z'}, representing the ii-th title. The sum of lengths of all titles does not exceed 3×1053 \times 10^5. Then follows a line with nn integers hih_i (1hi106)(1\leq h_i \leq 10^6), the happiness value of ii-th title. The next line is a single integer QQ (1Q3×105)(1 \leq Q \leq 3 \times 10^5), the number of queries. For the next QQ lines, each contains a single integer mm (1m106)(1 \leq m \leq 10^6), meaning that you can say no more than mm letters to ZYB. The input data contains only one test case.

Output

For each query, display a single line of integer, representing the answer. It can be proved that the answer can be uniquely written as p/qp/q where pp and qq are non-negative integers with gcd(p,q)=gcd(q,109+7)=1\gcd(p, q) = \gcd(q, 10^9 + 7) = 1, and you should display pq1mod(109+7)p \cdot q^{-1} \bmod (10^9 + 7), where q1q^{-1} means the multiplicative inverse of qq modulo 109+710^9 + 7.

Sample Input

2

zybnb

ybyb

3 5

4

1

2

3

4

Sample Output

769230776

425925929

891125950

633120399

Hint

For the first query, you can bring him 3 happiness points if you say "z" or "n", and 15 happiness points if you say "y" or "b"; all other strings of length 1 bring no happiness point to ZYB. Therefore, the expectation is (2¡Á3+2¡Á15)/26 = 18/13, and the answer is 18¡Á13^(-1) mod (10^9+7) = 769230776.

Source

2018 Multi-University Training Contest 8