#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;
// (]  >=
int main() {
    int64_t n;
    int64_t M = 1000000007;
    cin >> n;
    int64_t d = 1;
    int64_t c = 1;
    int64_t pow = 1;
    while (n) {
        if (n % 2) {
            d += c;
            c += pow;
        } else {
            c += d;
        }
        n /= 2;
        c += pow;
        pow *= 3;
        pow %= M;
        d %= M;
        c %= M;
    }
    cout << d;
    return 0;
}
