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

typedef long long ll;

const int mod = 1e9 + 7;

ll N;
vector<int> A;

int cc[62][2][2][2];
int dp(int n, int t, int d, int c) {
    if(n == A.size()) return t == 0 && d == 0 && c == 0;
    int &ret = cc[n][t][d][c];
    if(ret != -1) return ret;

    ret = 0;
    int x = A[n];
    for(int i = 0; i < 2; i++) {
        for(int j = 0; j < 2; j++) {
            if(i == 0 && j == 1) continue;

            int nt = ((i ^ j) > x) || (t & (i ^ j) == x);
            int nd = ((i + j + c) % 2 > x) || (d & (i + j + c) % 2 == x);

            ret += dp(n + 1, nt, nd, (i + j + c) / 2);
            ret %= mod;
        }
    }
    return ret;
}

int main() {
    cin >> N;

    while(N) {
        A.push_back(N % 2);
        N /= 2;
    }

    memset(cc, -1, sizeof(cc));
    printf("%d", dp(0, 0, 0, 0));
}
