This page is still under construction.

Parts of this page are still being built. What you see may change.

Tanya, Balls, and <<Exclusive Or>>

Time limit1sMemory limit512 MB

Summary
Compute the sum of bitwise XOR over all unordered pairs of the integers 1 to n, modulo 10^9+7, for n up to 10^9.
Level

Medium7 of 10

Topics
Bit manipulation, Math, Combinatorics, Divide and conquer
Solved
No attempts yet

Problem

Tanya had nn balls, numbered from 11 to nn. Unfortunately, Tanya dropped all the balls into a river and became very upset.

To comfort her, her older brother Seryozha suggested a fun mathematical pastime to Tanya: compute the sum of the pairwise exclusive or of her ball numbers.

The exclusive or of two numbers is denoted ⊕\oplus and corresponds to the <<xor>> operation in Pascal or <<\char 94>> in other languages. To compute x⊕yx \oplus y for two integers, do the following: write each number in binary, and set the ii-th bit of the result to one if that bit is one in exactly one of xx and yy. For example, 3⊕2=112⊕102=12=13 \oplus 2 = 11_2 \oplus 10_2 = 1_2 = 1, 17⊕5=100012⊕1012=101002=2017 \oplus 5 = 10001_2 \oplus 101_2 = 10100_2 = 20.

Help Tanya! Compute the sum over all pairs of her balls of the exclusive or of their numbers. Tanya does not like large numbers, so the answer must be printed modulo 109+710^9 + 7.

For example, if Tanya had 33 balls, the desired value is (1⊕2)+(1⊕3)+(2⊕3)=3+2+1=6(1 \oplus 2) + (1 \oplus 3) + (2 \oplus 3) = 3 + 2 + 1 = 6.

Input

The first line contains the number nn, the number of balls Tanya has (1≤n≤1091 \le n \le 10^9).

Output

Print the sum over all pairs of her balls of the exclusive or of their numbers, taken modulo 109+710^9 + 7.

Examples1

  1. Example 1

    Input
    3
    
    Expected output
    6