Under construction

Still being built. Things may change

Binary Tree

Time limit1sMemory limit1024 MB

Summary
For each tree T_i built by joining two earlier trees, sum the fewest subtrees needed to cover each contiguous leaf range exactly.
Level

Hard8 of 10

Topics
Tree, Dynamic programming, Math
Solved
No attempts yet

Definitions

For a binary tree TT in which every node has either 00 or 22 children, S(T)S(T) is defined as follows.

  • In TT, the subtree rooted at node uu consists of uu and the descendants of uu.
  • The inorder sequence p(T)p(T) of TT is the sequence of nodes in the order they are visited by an inorder traversal of TT. It is defined recursively.
    • Let rr be the root of TT. [r][r] is the sequence of length 11 that contains only rr.
    • If rr has 00 children, p(T)p(T) is [r][r].
    • If rr has 22 children, let XX be the subtree rooted at the left child of rr and YY the subtree rooted at the right child of rr. Then p(T)p(T) is p(X)p(X), [r][r], and p(Y)p(Y) concatenated in that order.
  • Let kk be the number of leaf nodes of TT. Number the leaves 1,2,⋯ ,k1, 2, \cdots, k in the order they appear in p(T)p(T), that is, in inorder visiting order.
  • Selecting a subtree of TT covers the leaf nodes contained in it.
  • For 1≤a≤b≤k1 ≤ a ≤ b ≤ k, f(a,b)f(a, b) is the minimum number of subtrees that must be selected in TT so that exactly the leaves numbered aa through bb are covered and no other leaf is covered.
  • S(T)S(T) is the sum of f(a,b)f(a, b) over all integer pairs (a,b)(a, b) with 1≤a≤b≤k1 ≤ a ≤ b ≤ k, taken modulo 109+710^9+7.

The tree in (a) is the tree built from the definition. (b) shows the same tree with its leaves numbered.

f(5,7)f(5, 7) is 22. Selecting the two subtrees shown below covers only leaves 55, 66, and 77.

Summing f(a,b)f(a, b) over all 1≤a≤b≤71 ≤ a ≤ b ≤ 7 gives 4747. Taking that modulo 109+710^9 + 7 gives S(T)=47S(T) = 47.

Problem

Integer sequences A1,A2,⋯ ,ANA_1, A_2, \cdots, A_N and B1,B2,⋯ ,BNB_1, B_2, \cdots, B_N are given.

Define binary trees T0,T1,⋯ ,TNT_0, T_1, \cdots, T_N as follows.

  • T0T_0 is a tree with one node.
  • For 1≤i≤N1 ≤ i ≤ N, TiT_i is the tree whose left child of the root roots the subtree TAiT_{A_i} and whose right child of the root roots the subtree TBiT_{B_i} (0≤Ai≤i−10 ≤ A_i ≤ i - 1, 0≤Bi≤i−10 ≤ B_i ≤ i - 1).

Write a program that computes S(T1),S(T2),⋯ ,S(TN)S(T_1), S(T_2), \cdots, S(T_N).

Input

The first line contains the integer NN. Each of the next NN lines contains AiA_i and BiB_i, separated by a space, for ii from 11 to NN.

Output

Print NN lines. On line ii (1≤i≤N1 ≤ i ≤ N), print S(Ti)S(T_i).

Constraints

  • All given numbers are integers.
  • 1≤N≤100 0001 ≤ N ≤ 100\,000
  • 0≤Ai≤i−10 ≤ A_i ≤ i - 1 (1≤i≤N1 ≤ i ≤ N)
  • 0≤Bi≤i−10 ≤ B_i ≤ i - 1 (1≤i≤N1 ≤ i ≤ N)

Examples2

  1. Example 1

    Input
    5
    0 0
    1 0
    1 2
    3 1
    4 4
    
    Expected output
    3
    7
    21
    47
    254
    
  2. Example 2

    Input
    7
    0 0
    1 1
    2 2
    3 3
    4 4
    5 5
    6 6
    
    Expected output
    3
    13
    65
    337
    1729
    8641
    41985