This page is still under construction.

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

Hashigo Sama

Time limit8sMemory limit256 MB

Summary
Count black-white colorings of joined ladders where each monochrome block has size at most k, modulo 1,000,000,007.
Level

Hard9 of 10

Topics
Dynamic programming, Graph
Solved
No attempts yet

Problem

Chelsea is a modern artist. She decided to build her next work out of ladders. She joins several ladders together and then paints a pattern on the result.

One ladder is described by a graph called a hashigo. There are nn hashigos, numbered 0 through n−1n-1. Hashigo ii has length lil_i and consists of the 2li+62l_i+6 vertices vi,0,vi,1,…,vi,2li+5v_{i,0}, v_{i,1}, \dots, v_{i,2l_i+5}. Its edges come in two kinds.

  • (vi,j,vi,j+2)(v_{i,j}, v_{i,j+2}) for every jj with 0≤j≤2li+30 \le j \le 2l_i+3
  • (vi,2j,vi,2j+1)(v_{i,2j}, v_{i,2j+1}) for every jj with 1≤j≤li+11 \le j \le l_i+1

The even numbered vertices are linked in the order vi,0,vi,2,…,vi,2li+4v_{i,0}, v_{i,2}, \dots, v_{i,2l_i+4} and form one rail, and the odd numbered vertices are linked in the order vi,1,vi,3,…,vi,2li+5v_{i,1}, v_{i,3}, \dots, v_{i,2l_i+5} and form the other rail. The two rails are connected by li+1l_i+1 rungs, and the four end vertices vi,0v_{i,0}, vi,1v_{i,1}, vi,2li+4v_{i,2l_i+4}, vi,2li+5v_{i,2l_i+5} carry no rung.

Hashigo ii and hashigo jj are combined at position pp (0≤p≤li−10 \le p \le l_i-1) and position qq (0≤q≤lj−10 \le q \le l_j-1) by merging each of the following four pairs into a single vertex.

(vi,2p+2,vj,2q+2),(vi,2p+3,vj,2q+4),(vi,2p+4,vj,2q+3),(vi,2p+5,vj,2q+5)(v_{i,2p+2}, v_{j,2q+2}), \quad (v_{i,2p+3}, v_{j,2q+4}), \quad (v_{i,2p+4}, v_{j,2q+3}), \quad (v_{i,2p+5}, v_{j,2q+5})

Chelsea performs this operation n−1n-1 times to combine all nn hashigos. After the operations the graph is connected and no vertex has degree greater than 4.

She now paints every vertex black or white, subject to one condition.

  • Every connected component formed by adjacent vertices of the same color has size at most kk.

She would like to inspect every pattern and pick the best one, but the number of patterns is very large. Count the colorings that satisfy the condition.

Input

The input contains several datasets. Each dataset has the following format.

n k
l0 l1 ... ln-1
f0 p0 t0 q0
...
fn-2 pn-2 tn-2 qn-2

The first line contains two integers nn (1≤n≤301 \le n \le 30) and kk (1≤k≤81 \le k \le 8).

The next line contains nn integers lil_i (1≤li≤301 \le l_i \le 30), the length of hashigo ii.

Each of the following n−1n-1 lines contains four integers fif_i (0≤fi≤n−10 \le f_i \le n-1), pip_i (0≤pi≤lfi−10 \le p_i \le l_{f_i}-1), tit_i (0≤ti≤n−10 \le t_i \le n-1), qiq_i (0≤qi≤lti−10 \le q_i \le l_{t_i}-1). It means that hashigo fif_i and hashigo tit_i are combined at position pip_i and position qiq_i. You may assume that the graph obtained by combining the nn hashigos is connected and that no vertex of the graph has degree greater than 4.

The last dataset is followed by a line containing two zeros.

Output

For each dataset, print the number of different colorings modulo 1,000,000,007 on one line.

Examples2

  1. Example 1

    Input
    1 5
    2
    3 7
    2 3 1
    0 1 1 0
    1 2 2 0
    2 8
    5 6
    0 2 1 2
    2 8
    1 1
    0 0 1 0
    2 2
    2 2
    0 1 1 0
    2 3
    3 3
    0 2 1 1
    2 4
    3 1
    1 0 0 1
    0 0
    
    Expected output
    708
    1900484
    438404500
    3878
    496
    14246
    9768
    
  2. Example 2

    Input
    1 1
    1
    2 1
    1 1
    0 0 1 0
    1 8
    1
    0 0
    
    Expected output
    2
    2
    256