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: Wrong Answer)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.Remoting.Contexts;
using System.Security.Cryptography.X509Certificates;

namespace Contest
{
    class Scanner
    {
        private string[] line = new string[0];
        private int index = 0;
        public string Next()
        {
            if (line.Length <= index)
            {
                line = Console.ReadLine().Split(' ');
                index = 0;
            }
            var res = line[index];
            index++;
            return res;
        }
        public int NextInt()
        {
            return int.Parse(Next());
        }
        public long NextLong()
        {
            return long.Parse(Next());
        }
        public string[] Array()
        {
            line = Console.ReadLine().Split(' ');
            index = line.Length;
            return line;
        }
        public int[] IntArray()
        {
            var array = Array();
            var result = new int[array.Length];
            for (int i = 0; i < array.Length; i++)
            {
                result[i] = int.Parse(array[i]);
            }

            return result;
        }
        public long[] LongArray()
        {
            var array = Array();
            var result = new long[array.Length];
            for (int i = 0; i < array.Length; i++)
            {
                result[i] = long.Parse(array[i]);
            }

            return result;
        }
    }

    class MyMath
    {
        static public long GCD(long a, long b)
        {
            while (b > 0)
            {
                var r = b;
                b = a % b;
                a = r;
            }
            return a;
        }

        static public long LCM(long a, long b)
        {
            return a * b / GCD(a, b);
        }
    }

    class Program
    {
        private long N;
        private const int Mod = 1000000007;
        private Dictionary<long, long> Map = new Dictionary<long, long>();
        private long[] Pow3 = new long[61];

        private void Scan()
        {
            var sc = new Scanner();
            N = sc.NextLong();
        }

        public void Solve()
        {
            Scan();
            Pow3[0] = 1;
            for (int i = 1; i <= 60; i++)
            {
                Pow3[i] = (Pow3[i - 1] * 3) % Mod;
            }
            Console.WriteLine(Ans(N));
        }

        private long Ans(long n)
        {
            long o;
            if (Map.TryGetValue(n, out o))
            {
                return o;
            }
            if (n < 0) return 0;
            if (n == 0)
            {
                return 1;
            }
            int i = B(n + 1);
            //Console.WriteLine($"{n} {i}");
            long a = PoW(i);
            long b = Ans(n - (1 << i));
            long c = Ans((1 << (i + 1)) - n - 3);
            long res = (a + b - c) % Mod;
            Map[n] = res;
            return res;
        }

        private int B(long b)
        {
            int i = 0;
            int a = 1;
            while (a <= b)
            {
                i++;
                a *= 2;
            }

            return i - 1;
        }

        private long PoW(long b)
        {
            return Pow3[b];
        }


        static void Main(string[] args)
        {
            new Program().Solve();
        }
    }
}