#include <bits/stdc++.h>
#include <algorithm>
#include <cmath>
using namespace std;
#define ll long long int
const ll mod = 1e9+7;
ll mul(ll a,ll b){
a%=mod;
b%=mod;
ll res = (a*b)%mod;
return res;
}
ll add(ll a,ll b){
ll res = (a+b+mod)%mod;
return res;
}
ll binpow(ll a,ll b){
ll res = 1;
while(b>0){
if(b&1)res = mul(res,a);
a = mul(a,a);
b>>=1;
}
return res;
}
ll inv(ll a){
return binpow(a,mod-2);
}
ll series(ll a,ll r,ll n){
ll ans = mul(a,add(1,-binpow(r,n)));
ans = mul(ans,inv(add(1,-r)));
return ans%mod;
}
int main(){
ll n;
cin>>n;
// cout<<series(1,2,2);
ll num = n;
ll bi = 0;
while(num!=0){
num/=2;
bi++;
}
ll p = 4;
ll prev = 27;
ll ans=0;
for(int i = 3;i<=bi;i++){
ll to=0,las;
if(i==bi)to = n-p+1,las = n;
else to = 2*p-p,las=2*p-1;
ll aa = binpow(2,mul(to,i));
// cout<<aa<<" "<<prev*aa<<" s\n";
ans = mul(prev,aa);
ll a = inv(binpow(2,i));
ll r = a;
ll fir = add(p,-mul(las,inv(binpow(2,mul(to,i)))));
ll cur = 1;
ll bb = mul(aa,inv(add(binpow(2,i),-1)));
if(to>=1){
cur = add(fir,series(a,r,to-1));
cur = mul(cur,bb);
}
ans += (cur);
ans%=mod;
p*=2;
prev=ans;
}
cout<<ans;
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNpbmNsdWRlIDxhbGdvcml0aG0+CiNpbmNsdWRlIDxjbWF0aD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2RlZmluZSBsbCBsb25nIGxvbmcgaW50Cgpjb25zdCBsbCBtb2QgPSAxZTkrNzsKCgpsbCBtdWwobGwgYSxsbCBiKXsKICAgIGElPW1vZDsKICAgIGIlPW1vZDsKICAgIGxsIHJlcyA9IChhKmIpJW1vZDsKICAgIHJldHVybiByZXM7Cn0KCmxsIGFkZChsbCBhLGxsIGIpewogICAgbGwgcmVzID0gKGErYittb2QpJW1vZDsKICAgIHJldHVybiByZXM7Cn0KCmxsIGJpbnBvdyhsbCBhLGxsIGIpewogICAgbGwgcmVzID0gMTsKICAgIHdoaWxlKGI+MCl7CiAgICAgICAgaWYoYiYxKXJlcyA9IG11bChyZXMsYSk7CiAgICAgICAgYSA9IG11bChhLGEpOwogICAgICAgIGI+Pj0xOwogICAgfQogICAgcmV0dXJuIHJlczsKfQoKCmxsIGludihsbCBhKXsKICAgIHJldHVybiBiaW5wb3coYSxtb2QtMik7Cn0KCmxsIHNlcmllcyhsbCBhLGxsIHIsbGwgbil7CiAgICBsbCBhbnMgPSBtdWwoYSxhZGQoMSwtYmlucG93KHIsbikpKTsKICAgIGFucyA9IG11bChhbnMsaW52KGFkZCgxLC1yKSkpOwogICAgcmV0dXJuIGFucyVtb2Q7Cn0KCmludCBtYWluKCl7CiAgICBsbCBuOwogICAgY2luPj5uOwogICAgLy8gY291dDw8c2VyaWVzKDEsMiwyKTsKICAgIGxsIG51bSA9IG47CiAgICBsbCBiaSA9IDA7CiAgICB3aGlsZShudW0hPTApewogICAgICAgIG51bS89MjsKICAgICAgICBiaSsrOwogICAgfQogICAgbGwgcCA9IDQ7CiAgICBsbCBwcmV2ID0gMjc7CiAgICBsbCBhbnM9MDsKICAgIGZvcihpbnQgaSA9IDM7aTw9Ymk7aSsrKXsKICAgICAgICBsbCB0bz0wLGxhczsKICAgICAgICBpZihpPT1iaSl0byA9IG4tcCsxLGxhcyA9IG47CiAgICAgICAgZWxzZSB0byA9IDIqcC1wLGxhcz0yKnAtMTsKICAgICAgICBsbCBhYSA9IGJpbnBvdygyLG11bCh0byxpKSk7CiAgICAgICAgLy8gY291dDw8YWE8PCIgIjw8cHJldiphYTw8IiBzXG4iOwogICAgICAgIGFucyA9IG11bChwcmV2LGFhKTsKICAgICAgICBsbCBhID0gaW52KGJpbnBvdygyLGkpKTsKICAgICAgICBsbCByID0gYTsKICAgICAgICBsbCBmaXIgPSBhZGQocCwtbXVsKGxhcyxpbnYoYmlucG93KDIsbXVsKHRvLGkpKSkpKTsKICAgICAgICBsbCBjdXIgPSAxOwogICAgICAgIGxsIGJiID0gbXVsKGFhLGludihhZGQoYmlucG93KDIsaSksLTEpKSk7CiAgICAgICAgaWYodG8+PTEpewogICAgICAgICAgICBjdXIgPSBhZGQoZmlyLHNlcmllcyhhLHIsdG8tMSkpOwogICAgICAgICAgICBjdXIgPSBtdWwoY3VyLGJiKTsKICAgICAgICB9CiAgICAgICAgYW5zICs9IChjdXIpOwogICAgICAgIGFucyU9bW9kOwogICAgICAgIHAqPTI7CiAgICAgICAgcHJldj1hbnM7CiAgICB9CiAgICBjb3V0PDxhbnM7CiAgICByZXR1cm4gMDsKfQoK