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 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)
        {
            //Console.WriteLine(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 - (1l << i));
            long c = Ans((1l << (i + 1)) - n - 3);
            long res = (a + b - c) % Mod;
            while (res < 0)
            {
                res += Mod;
            }
            Map[n] = res;
            return res;
        }

        private int B(long b)
        {
            int i = 0;
            long 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();
        }
    }
}

Measured memory_bytes: 13268