#include <bits/stdc++.h>
using namespace std;

int main(){
    long long int N, M, m = 1000000007, a[64], k = 1, l = 0, c = 0, i, o = 1;
    scanf("%lld", &N);
    M = ((++N)^(N/2));
    for(i = 64; i; ++c){
        if(M & (o << --i)){
            swap(k, l);
            l += k * c;
            l %= m;
            c = 0;
        }
        a[i] = (N & (o << i))?l:(-l);
    }
    i = 0;
    for(k = 0, l = 1; i < 64 - __builtin_clzll(M); ++i){
        k += a[i] * l;
        l *= 3;
        k %= m;
        l %= m;
    }
    cout << (k + m) % m << endl;
    return 0;
}
