- Time limit: 2.00 s
- Memory limit: 512 MB
Uolevi has a perfect binary tree of size . He wants you to answer queries of the following form: Given the index of a node in the inorder traversal of the tree, what is the index of the node in the breadth-first traversal of the tree?
Since the amount of output could be very large, output the sum of all answers to queries mod .
In inorder traversal, first all left children appear (in inorder), then the node appears, then all right children appear (in inorder).
Here is the inorder indexing of a perfect binary tree of size 7:
In breadth-first traversal, a node appears before another node if or and there is some node where appears in the node's left subtree, and appears in the node's right subtree.
Breadth-first indexing of the same tree:
Both traversals start from the root of the tree, and are 1-indexed.
Input
The first line of input contains two integers and : Uolevi's tree has size , and there are queries.
The second line contains the queries, each a value : What is the index of the node, whose index in the inorder traversal of the tree is , in the breadth-first traversal of the tree?
Output
Output a single integer: The sum of the answers to all queries mod . The answer to each query is the index of the node in the tree's breadth-first traversal.
Constraints
Example
Input:
3 7 1 2 3 4 5 6 7
Output:
28
Explanation:
The answers to the queries are:
4 2 5 1 6 3 7