#include <iostream>
#include <fstream>
// #include <bits/stdc++.h>
#include <sstream>
#include <list>
#include <stack>
#include <deque>
#include <utility>
#include <queue>
#include <set>
#include <map>
#include <assert.h>
#include <bitset>
#include <vector>
#include <cmath>
#include <string>
#include <algorithm>
#include <iomanip>
#include <ctime>
#include <iterator>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <complex>
#define f first
#define s second
#define ll long long
#define ld long double
#define pb push_back
#define files1 freopen("input.txt","r",stdin)
#define files2 freopen("output.txt","w",stdout)
#define files files1;files2
#define mp make_pair
#define fast_io ios_base::sync_with_stdio(0);cin.tie(0)
#define forn() for(int i=0;i<n;i++)
#define vi vector<int>
#define pii pair<int,int>
#define endl '\n'
#define fill(x) memset((x), 0, sizeof((x)))
using namespace std;
const int inf = 2e9;
const double eps = 1e-9;
const int maxn = 2e5 + 10, base = 1e9 + 7;
const int sigm = 26;
const ll llinf = 1e18;
void bad(string mes = "Impossible"){cout << mes;exit(0);}
template<typename T>
string bin(T x){
string ans = "";
while (x > 0){
ans += char('0' + x % 2);
x /= 2;
}
reverse(ans.begin(), ans.end());
return ans.empty() ? "0" : ans;
}
template<typename T>
T dcm(string & s)
{
T x = 0;
for (int i = 0; i < s.size(); i++){
x = (x * 2) + (s[i] == '1');
}
return x;
}
template<typename T>
T input(){
T ans = 0, m = 1;
char c = ' ';
while (c == ' ' || c == '\n')
c = getchar();
if (c == '-')
m = -1, c = getchar();
while (c >= '0' && c <= '9'){
ans = ans * 10 + int(c - '0'), c = getchar();
}
return ans * m;
}
template<typename T>
T binpow(T n, T s)
{
if (s == 0)
return 1LL;
if (s % 2 == 0){
T b = binpow(n, s / 2);
return ( 1LL * b * b );
} else {
return (1LL* binpow(n, s - 1) * n);
}
}
ll dp[64][64][2]; // the last digit in pattern -> pattern % 2
ll realPat[64];
int main()
{
ll n;
cin >> n;
int len = bin(n).size();
if (n == 1)
bad("1");
dp[len - 1][1][1] = 1; // dp[len][pattern][prefix]
for (int byte = len - 1; byte > 0; byte--){
int curLet = (((ll)n & (1LL << (byte - 1))) != 0);
for (int pattern = 1; pattern < 62; pattern++){
int inThePattern = pattern % 2;
for (int lowerNew = 0; lowerNew < 2; lowerNew++){
int weAreGoing = pattern + (lowerNew != inThePattern);
if (weAreGoing >= 62)
continue;
dp[byte - 1][weAreGoing][0] = (dp[byte - 1][weAreGoing][0] + dp[byte][pattern][0]) % base;
if (lowerNew < curLet)
dp[byte - 1][weAreGoing][0] = (dp[byte - 1][weAreGoing][0] + dp[byte][pattern][1]) % base;
if (lowerNew == curLet)
dp[byte - 1][weAreGoing][1] = (dp[byte - 1][weAreGoing][1] + dp[byte][pattern][1]) % base;
}
}
}
realPat[1] = 1;
for (int i = 2; i < 62; i++){
realPat[i] = (realPat[i - 1] * 2LL + (i % 2LL)) % base;
}
ll ans = 0;
for (int i = 1; i < 62; i++){
ans = (ans + (dp[0][i][0] * realPat[i]) % base) % base;
ans = (ans + (dp[0][i][1] * realPat[i]) % base) % base;
}
fill(dp);
len--;
dp[len - 1][1][0] = 1;
//ans for all lens 0..len - 1
for (int byte = len - 1; byte >= 0; byte--){
for (int pattern = 1; pattern < 62; pattern++){
int inThePattern = pattern % 2;
ans = (ans + (dp[byte][pattern][0] * realPat[pattern]) % base) % base;
if (byte){
for (int lowerNew = 0; lowerNew < 2; lowerNew++){
int weAreGoing = pattern + (lowerNew != inThePattern);
if (weAreGoing >= 62)
continue;
dp[byte - 1][weAreGoing][0] = (dp[byte - 1][weAreGoing][0] + dp[byte][pattern][0]) % base;
}
}
}
}
cout << ans;
return 0;
}