Crazy Tree 3

3.9

452 votes
Modular exponentiation, Medium, Modular arithmetic, Approved, Mathematics
Problem

Abhimanyu has a complete binary tree of level L, called Crazy Tree. Levels are numbered 1, 2, ..., L from top to bottom and root is at level 1. Abhimanyu numbers all the leaf nodes 1, 2, 3, ... from left to right. The value of the parent node is the product of values of its child node.

Below are level 2 (left) and level 3 (right) crazy trees

Level 2 Level 3

Abhimanyu defines a magical function S:

  • S(N, X, Y): This gives the sum of Xth, (X + 1)th, (X + 2)th, ..., Yth nodes at level N.

Abhimanyu wants to find the value of S(N, X, Y) % M for given values of N, X, Y, where M = 1299709.

Input

First line of input contains two space separated integers L and Q, where L is number of levels in the crazy tree and Q is total number of queries. Each of the next Q lines contains three space separated integers N, X and Y.

Output

Output the value of S(N, X, Y) % M in single line for each test case.

Constraints

  • 1 <= L <= 21
  • 1 <= Q <= min(106, (2L - 1) (2L + 4) / 6), where min(a, b) is minimum of a and b
  • 1 <= N <= L
  • 1 <= X <= 2(L - 1)
  • X <= Y <= 2(L - 1)
Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

As Tree has 3 levels you can refer to right image above.

Now for query 2 1 2 At level 2, Value of node 1 = 2 At level 2, value of node 2 = 12

So, S(2, 1, 2) = 2 + 12 = 14

Editor Image

?