Crazy Tree 3
Practice
3.9 (452 votes)
Modular exponentiation
Medium
Modular arithmetic
Approved
Mathematics
Problem
79% Success 2301 Attempts 30 Points 2s Time Limit 256MB Memory 1024 KB Max Code

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)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
8 votes
Tags:
ApprovedMathMediumOpen
Points:30
19 votes
Tags:
Dynamic ProgrammingMathMediumNumber Theory
Points:20
1 votes
Tags:
EasySimple-math