Under construction

Still being built. Things may change

New AVL Tree

Time limit2sMemory limit1024 MB

Summary
Given a set S of allowed balance values and a height h, count the tree shapes of height h where every non-leaf node has a balance in S, modulo 1e9+7.
Level

Medium5 of 10

Topics
Tree, Dynamic programming
Solved
No attempts yet

Problem

The AVL tree is named after Adelson-Velsky and Landis, who created it.

To define an AVL tree, first define the "balance value" of each node. The balance value of a node is hr−hlh_r-h_l, where hlh_l is the height of its left subtree and hrh_r is the height of its right subtree. If the node has no left or right child, the height of that subtree is taken as 00. In a regular AVL tree, every node has a balance value of −1-1, 00, or 11.

Minjae, who attended the MatKor data structures seminar, wants to develop an AVLM tree named after himself. He allows only some of the values −1-1, 00, and 11 as balance values. He picks a nonempty subset SS of {−1,0,1}\{-1,0,1\} and defines an AVLM tree as a tree in which every node except the leaves has a balance value that is an element of SS. Leaves are exempt because their balance value is always 00.

Given the set SS and a height hh of an AVLM tree, count the number of possible tree shapes of height hh.

A tree with a single node has height 11.

Input

The first line contains the number of test cases TT. (1≤T≤1061\le T\le 10^6)

Each test case consists of two lines. The first line contains the height hh and the number of elements ∣S∣\lvert S \rvert, separated by a space. (1≤h≤1061\le h\le 10^6, 1≤∣S∣≤31\le\lvert S \rvert\le 3)

The second line contains the ∣S∣\lvert S \rvert distinct elements of SS, which are the allowed balance values of the nodes, in ascending order.

The set SS is one of {−1}\{-1\}, {0}\{0\}, {1}\{1\}, {−1,0}\{-1,0\}, {−1,1}\{-1,1\}, {0,1}\{0,1\}, {−1,0,1}\{-1,0,1\}.

Each test case appears only once.

Output

For each test case, print the answer modulo 109+710^9+7 on its own line.

Hint

Reading input with fast I/O is necessary when there are many test cases, or the program may exceed the time limit.

Examples1

  1. Example 1

    Input
    6
    1 1
    1
    2 1
    -1
    3 2
    -1 1
    3 3
    -1 0 1
    5 3
    -1 0 1
    1000000 3
    -1 0 1
    
    Expected output
    1
    1
    4
    15
    108675
    431215210