#include <bits/stdc++.h>
using namespace std;
template <typename T>
void readv(vector<T> &a) {
  for (long long i = (0); i < (a.size()); i++) cin >> a[i];
}
void readi(vector<long long> &a) {
  for (long long i = (0); i < (a.size()); i++) {
    cin >> a[i];
    a[i]--;
  }
}
long long mod = 1000000007;
long long pow2[64];
long long pow3[64];
void count_uv(long long N, long long d, long long &ans) {
  if (d == -1) {
    ans++;
    ans %= mod;
    return;
  }
  for (long long a = (0); a < (3); a++) {
    long long N0 = N - pow2[d] * a;
    if (N0 < 0) return;
    if (N0 >= 2 * (pow2[d] - 1)) {
      ans += pow3[d];
      ans %= mod;
    } else {
      count_uv(N0, d - 1, ans);
    }
  }
}
signed main() {
  long long N;
  cin >> N;
  pow2[0] = 1;
  for (long long i = (1); i < (64); i++) pow2[i] = 2 * pow2[i - 1];
  pow3[0] = 1;
  for (long long i = (1); i < (64); i++) pow3[i] = (3 * pow3[i - 1]) % mod;
  long long D;
  for (long long i = (0); i < (64); i++)
    if (pow2[i] > N) {
      D = i - 1;
      break;
    }
  long long ans = 0;
  count_uv(N, D, ans);
  cout << ans;
  return 0;
}
