Under construction

Still being built. Things may change

Drawing Binary Trees

Time limit1sMemory limit512 MB

Summary
Count the x-coordinate assignments for a binary tree's nodes in [1, V] that keep the left-below, right-above ordering and match the fixed values in A, modulo 444449.
Level

Medium7 of 10

Topics
Tree, Dynamic programming, Combinatorics
Solved
No attempts yet

Problem

Bert has recently become fascinated with binary trees. For a tree to be a binary tree, each node may have at most 2 children. For convenience, the nodes of a binary tree TT are numbered 1 through NN. The left child of node ii is written L_iL\_i, and the right child is written R_iR\_i. If a node has no child on that side, the value is 0.

The tree below has N=3N = 3, L=[2,0,0]L = [2, 0, 0], and R=[3,0,0]R = [3, 0, 0]. Its root is node 1.

Bert likes to draw binary trees. He first fixes an integer V≥NV \ge N that is large enough, then draws the tree in several ways by the rules below. Let the integer coordinates of node ii be (xi,yi)(x_i, y_i).

  1. Choose xix_i so that 1≤xi≤V1 \le x_i \le V.
  2. If node ii is the root, then yi=−1y_i = -1.
  3. If node pp is the parent of node ii, then yi=yp−1y_i = y_p - 1.
  4. If a non-root node ii is the left child of node pp (that is, Lp=iL_p = i), then xi<xpx_i < x_p must hold. Also, xj<xpx_j < x_p must hold for every child and descendant jj of ii.
  5. If a non-root node ii is the right child of node pp (that is, Rp=iR_p = i), then xi>xpx_i > x_p must hold. Also, xj>xpx_j > x_p must hold for every child and descendant jj of ii.

When NN coordinates satisfy all the rules above, the binary tree can be drawn. Two drawings are different if any node has a different xx coordinate in them. Under these rules, every node has the same yy coordinate in every drawing.

For the example above with V=4V = 4, the tree can be drawn in 4 different ways, as shown below.

Given any binary tree, Bert wondered how many different drawings follow all of the rules above. Alice saw this and called that problem too easy, so she gave him a new one.

  • If the xx coordinates of some of the NN nodes (possibly none, possibly all) are fixed, how many different drawings are possible when the rules above are followed?
  • Specifically, an integer array AA is given. If Ai=0A_i = 0, the xx coordinate of node ii is not fixed. If Ai∈[1,V]A_i \in [1, V], then the xx coordinate of node ii must be exactly xi=Aix_i = A_i.

For example, if A=[0,1,0]A = [0, 1, 0], the tree can be drawn in the 3 leftmost ways among the 4 drawings above, which are the ones with x2=1x_2 = 1. For the same tree with A=[1,0,0]A = [1, 0, 0], there is no valid drawing. The tree can be drawn in 4 ways as shown above, but none of them has x1=1x_1 = 1.

Given NN, VV, and three integer arrays AA, LL, and RR, count the number of ways to draw the binary tree that satisfy Alice's conditions.

Input

The first line contains the number of test cases TT.

The first line of each test case contains NN and VV, separated by a space. The second line contains the array AA, the third line contains the array LL, and the fourth line contains the array RR. Each array is given as NN integers separated by spaces.

Output

For each test case, print the answer on its own line. The answer can be very large, so print the remainder when it is divided by 444 449444\,449.

Constraints

  • 1≤T≤101 \le T \le 10
  • 1≤N≤100 0001 \le N \le 100\,000
  • N≤V≤1 000 000 000N \le V \le 1\,000\,000\,000
  • For every ii with 1≤i≤N1 \le i \le N:
    • 0≤Ai≤V0 \le A_i \le V
    • 0≤Li,Ri≤N0 \le L_i, R_i \le N
  • The input binary tree has NN nodes and N−1N-1 edges, has a unique root, and contains no cycles. This is guaranteed.

Examples1

  1. Example 1

    Input
    6
    3 4
    0 0 0
    2 0 0
    3 0 0
    3 4
    1 0 0
    2 0 0
    3 0 0
    3 4
    0 1 0
    2 0 0
    3 0 0
    5 7
    0 4 0 0 0
    0 3 0 0 0
    2 4 0 5 0
    5 10
    0 5 0 0 0
    0 3 0 0 0
    2 4 0 5 0
    6 10
    0 0 3 6 0 8
    2 3 0 0 6 0
    0 5 4 0 0 0
    
    Expected output
    4
    0
    3
    9
    60
    1