Problem:
Score : 600 points

Problem Statement

You are given a positive integer N.
Find the number of the pairs of integers u and v (0≦u,v≦N) such that there exist two non-negative integers a and b satisfying a xor b=u and a+b=v.
Here, xor denotes the bitwise exclusive OR.
Since it can be extremely large, compute the answer modulo 10^9+7.

Constraints

1≦N≦10^{18}

Input

The input is given from Standard Input in the following format:

N

Output

Print the number of the possible pairs of integers u and v, modulo 10^9+7.

Sample Input 1

3

Sample Output 1

5

The five possible pairs of u and v are:

u=0,v=0 (Let a=0,b=0, then 0 xor 0=0, 0+0=0.)

u=0,v=2 (Let a=1,b=1, then 1 xor 1=0, 1+1=2.）

u=1,v=1 (Let a=1,b=0, then 1 xor 0=1, 1+0=1.）

u=2,v=2 (Let a=2,b=0, then 2 xor 0=2, 2+0=2.）

u=3,v=3 (Let a=3,b=0, then 3 xor 0=3, 3+0=3.）

Sample Input 2

1422

Sample Output 2

52277

Sample Input 3

1000000000000000000

Sample Output 3

787014179

Solution in C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
//using System.Text;
//using System.Text.RegularExpressions;
//using System.Globalization;
//using System.Diagnostics;
using static System.Console;
//using System.Numerics;
//using static System.Math;
//using pair = Pair<int, int>;

class Program
{
    static void Main()
    {
        //SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false });
        new Program().solve();
        Out.Flush();
    }
    Scanner cin = new Scanner();
    readonly int[] dd = { 0, 1, 0, -1, 0 }; //→↓←↑
    readonly int mod = 1000000007;


    void solve()
    {
        //int N = cin.nextint;
        //var A = new bool[N * 10, N * 10];
        //for (int i = 0; i < N; i++)
        //{
        //    for (int j = i; j < N; j++)
        //    {
        //        int u = i ^ j;
        //        int v = i + j;
        //        A[u, v] = true;
        //        //WriteLine($"a={i} b={j} u={u} v={v}");
        //    }
        //}
        //
        //int sum = 0;
        //for (int r = 0; r < N; r++)
        //{
        //    int ans = 0;
        //    for (int i = 0; i <= r; i++)
        //    {
        //        for (int j = 0; j <= r; j++)
        //        {
        //            if (A[i, j]) ans++;
        //        }
        //    }
        //    WriteLine(ans);
        //}

        var N = cin.nextlong;
        D = new Dictionary<long, long>() { [0] = 0, [1] = 1 };
        WriteLine(calc(N + 1));
    }
    Dictionary<long, long> D;
    long calc(long u)
    {
        if (D.ContainsKey(u)) return D[u];
        if (u % 2 == 0)
        {
            D[u] = (2 * calc(u / 2) % mod + calc(u / 2 - 1)) % mod;
            return D[u];
        }
        else
        {
            D[u] = (2 * calc(u / 2) % mod + calc(u / 2 + 1)) % mod;
            return D[u];
        }
    }

}

class Scanner
{
    string[] s; int i;
    char[] cs = new char[] { ' ' };
    public Scanner() { s = new string[0]; i = 0; }
    public string[] scan { get { return ReadLine().Split(); } }
    public int[] scanint { get { return Array.ConvertAll(scan, int.Parse); } }
    public long[] scanlong { get { return Array.ConvertAll(scan, long.Parse); } }
    public double[] scandouble { get { return Array.ConvertAll(scan, double.Parse); } }
    public string next
    {
        get
        {
            if (i < s.Length) return s[i++];
            string st = ReadLine();
            while (st == "") st = ReadLine();
            s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
            i = 0;
            return next;
        }
    }
    public int nextint { get { return int.Parse(next); } }
    public long nextlong { get { return long.Parse(next); } }
    public double nextdouble { get { return double.Parse(next); } }
}