Under construction

Still being built. Things may change

Infinite Binary Embedding

Time limit2sMemory limit256 MB

Summary
Count the ways to embed a finite binary tree into the infinite binary tree so that each leaf lands at a prescribed height, modulo 1e9+7.
Level

Hard8 of 10

Topics
Tree, Dynamic programming, Combinatorics, Math
Solved
No attempts yet

Problem

Vasya has an infinite binary tree TT, which can be formally described as follows. TT has infinitely many vertices, numbered starting from 1. Each vertex of TT has two children: a left son and a right son. The left son of vertex ii is vertex 2i2i, and the right son of vertex ii is vertex 2i+12i + 1.

Vasya also has another binary tree GG (a finite one, fortunately). Every vertex of GG is either an internal vertex or a leaf. An internal vertex has a left son and a right son (and is called the parent of its children), and a leaf vertex has no children. Each vertex has exactly one parent, except for one vertex with no parent; this vertex is called the root of tree GG.

Vasya wants to embed tree GG in the infinite tree TT. Formally, an embedding is a function f:V(G)→V(T)f: V(G) \rightarrow V(T) (here V(X)V(X) is the set of vertices of tree XX) with the following property: if vertex vv is a left (alternatively: right) son of vertex uu in GG, then vertex f(v)f(v) must lie in the subtree of the left (alternatively: right) son of vertex f(u)f(u) in TT.

Informally, an embedding is a way to place the vertices of GG somewhere on TT so that if we draw paths between all the embedded vertices and their children, the drawing looks like GG with some of the edges possibly extended downward (looking at the pictures for the sample cases might help to understand the notion better).

Additionally, for each leaf vertex vv Vasya has chosen a number hvh_v, the height of the vertex of TT that vv should be embedded to (the height of a vertex is the number of edges between the vertex and the root of the tree). That is, height(f(v))=hv\text{height}(f(v)) = h_v must hold for every leaf vv of tree GG.

Now Vasya wants to know the number of different embeddings of GG in TT such that each leaf vv is embedded to a vertex with height hvh_v. As the number may be quite large, find it modulo 109+710^9 + 7.

Input

The first line contains one integer nn, the number of vertices of GG (1≤n≤20001 \leq n \leq 2000).

The next nn lines describe the tree GG and the numbers hvh_v.

If the ii-th vertex is an internal vertex, then the first number in the ii-th line is 00, followed by the numbers of its left and right sons respectively.

If the ii-th vertex is a leaf, then the first number in the ii-th line is 11, followed by the number hih_i for this leaf. All hih_i satisfy 0≤hi≤1090 \leq h_i \leq 10^9.

The root of GG is vertex 11.

Output

Print one integer: the number of appropriate embeddings modulo 109+710^9 + 7.

Notes

All six possible embeddings for the first sample (note that the root of GG does not have to coincide with the root of TT):

All fourteen possible embeddings for the second sample:

Examples4

  1. Example 1

    Input
    3
    0 2 3
    1 2
    1 2
    
    Expected output
    6
    
  2. Example 2

    Input
    5
    0 2 3
    0 4 5
    1 2
    1 3
    1 3
    
    Expected output
    14
    
  3. Example 3

    Input
    3
    0 2 3
    1 0
    1 0
    
    Expected output
    0
    
  4. Example 4

    Input
    1
    1 0
    
    Expected output
    1