#include <bits/stdc++.h>
using namespace std;
template <typename T>
void vec_print(vector<T> VEC) {
  for (long long i = (long long)0; i < (long long)VEC.size(); ++i) {
    cout << VEC[i] << " ";
  }
  cout << "\n";
};
template <typename T>
void mat_print(vector<vector<T>> MAT) {
  for (long long i = (long long)0; i < (long long)MAT.size(); ++i) {
    for (long long j = (long long)0; j < (long long)MAT[i].size(); ++j) {
      cout << MAT[i][j] << " ";
    }
    cout << "\n";
  }
};
template <typename CLASS1, typename CLASS2>
class HOGE {
 public:
  CLASS1 key;
  CLASS2 value;
  HOGE(void) { return; };
  HOGE(CLASS1 key, CLASS2 value) {
    this->key = key;
    this->value = value;
  };
  ~HOGE(void) { return; };
  void print(void) {
    cout << "key : " << key << ", value : " << value << "\n";
    return;
  };
  bool operator==(const HOGE &obj) { return (this->value == obj.value); };
  bool operator<(const HOGE &obj) { return (this->value < obj.value); };
  bool operator>(const HOGE &obj) { return (this->value > obj.value); };
};
constexpr int INF = (1 << 30);
constexpr long long INFLL = 1LL << 62;
constexpr long double EPS = 1e-12;
constexpr long long MOD = (long long)((1E+9) + 7);
unordered_map<long long, long long> mp;
long long sequential(long long N) {
  if (mp.find(N) != mp.end()) return mp[N];
  sequential(N / 2);
  if (N % 2 == 1) return mp[N] = mp[N / 2];
  sequential(N / 2 - 1);
  return mp[N] = mp[N / 2] + mp[N / 2 - 1];
};
long long sum(long long N) {
  if (N == 0) return mp[0];
  if (N == 1) return mp[0] + mp[1];
  sequential(N / 2);
  if (N % 2 == 0) return (3 * sum(N / 2 - 1) + mp[N / 2]) % MOD;
  return (3 * sum(N / 2 - 1) + 2 * mp[N / 2]) % MOD;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long N;
  cin >> N;
  mp[0] = 1;
  cout << sum(N) % MOD << "\n";
  return 0;
}
