/** * @FileName a.cpp * @Author kanpurin * @Created 2022.11.06 11:42:45 **/ #include "bits/stdc++.h" using namespace std; typedef long long ll; template< int MOD > struct mint { public: unsigned int x; mint() : x(0) {} mint(long long v) { long long w = (long long)(v % (long long)(MOD)); if (w < 0) w += MOD; x = (unsigned int)(w); } mint(std::string &s) { unsigned int z = 0; for (int i = 0; i < s.size(); i++) { z *= 10; z += s[i] - '0'; z %= MOD; } x = z; } mint operator+() const { return *this; } mint operator-() const { return mint() - *this; } mint& operator+=(const mint &a) { if ((x += a.x) >= MOD) x -= MOD; return *this; } mint& operator-=(const mint &a) { if ((x -= a.x) >= MOD) x += MOD; return *this; } mint& operator*=(const mint &a) { unsigned long long z = x; z *= a.x; x = (unsigned int)(z % MOD); return *this; } mint& operator/=(const mint &a) {return *this = *this * a.inv(); } friend mint operator+(const mint& lhs, const mint& rhs) { return mint(lhs) += rhs; } friend mint operator-(const mint& lhs, const mint& rhs) { return mint(lhs) -= rhs; } friend mint operator*(const mint& lhs, const mint& rhs) { return mint(lhs) *= rhs; } friend mint operator/(const mint& lhs, const mint& rhs) { return mint(lhs) /= rhs; } friend bool operator==(const mint& lhs, const mint& rhs) { return lhs.x == rhs.x; } friend bool operator!=(const mint& lhs, const mint& rhs) { return lhs.x != rhs.x; } friend std::ostream& operator<<(std::ostream &os, const mint &n) { return os << n.x; } friend std::istream &operator>>(std::istream &is, mint &n) { unsigned int x; is >> x; n = mint(x); return is; } mint inv() const { assert(x); return pow(MOD-2); } mint pow(long long n) const { assert(0 <= n); mint p = *this, r = 1; while (n) { if (n & 1) r *= p; p *= p; n >>= 1; } return r; } mint sqrt() const { if (this->x < 2) return *this; if (this->pow((MOD-1)>>1).x != 1) return mint(0); mint b = 1, one = 1; while (b.pow((MOD-1) >> 1) == 1) b += one; long long m = MOD-1, e = 0; while (m % 2 == 0) m >>= 1, e += 1; mint x = this->pow((m - 1) >> 1); mint y = (*this) * x * x; x *= (*this); mint z = b.pow(m); while (y.x != 1) { int j = 0; mint t = y; while (t != one) j += 1, t *= t; z = z.pow(1LL << (e-j-1)); x *= z; z *= z; y *= z; e = j; } return x; } }; constexpr int MOD = 1e9 + 7; struct Monoid { using T = mint; T val; bool undef = true; Monoid() { *this = zero(); } Monoid(T val, bool undef = true) : val(val), undef(undef) {} static Monoid zero() { return Monoid(0); } static Monoid e() { return Monoid(1,false); } Monoid& operator+=(const Monoid &a) { if (this->undef) *this = a; else if (!a.undef) this->val += a.val; return *this; } Monoid& operator*=(int c) { return *this; } friend Monoid operator+(const Monoid& a, const Monoid& b) { return Monoid(a) += b; } friend Monoid operator*(const Monoid& a, int c) { return Monoid(a) *= c; } friend std::ostream& operator<<(std::ostream &os, const Monoid &x) { return os << x.val; } }; struct Automaton { std::vector> delta; std::vector is_accept; int qsize; int init; int alphabet_size = 10; inline int next(int state, int c) const { return delta[state][c]; } inline bool accept(int state) const { return is_accept[state]; } inline int size() const {return qsize; } }; struct LeqADFA : public Automaton { private: std::string str; bool eq; void initializer() { qsize = (str.size()+1)*2; init = 0; set_delta(); set_is_accept(); } void set_delta() { delta.resize(qsize,std::vector(alphabet_size,0)); for (int i = 0; i < (int)str.size(); i++) { int state = i<<1; delta[state][str[i]-'0'] = state+2; for (int c = 0; c < str[i]-'0'; c++) { delta[state][c] = state+1; } for (int c = str[i]-'0'+1; c < alphabet_size; c++) { delta[state][c] = qsize-1; } for (int c = 0; c < alphabet_size; c++) { delta[state+1][c] = state+3; } } for (int c = 0; c < alphabet_size; c++) { delta[qsize-2][c] = qsize-1; delta[qsize-1][c] = qsize-1; } } void set_is_accept() { is_accept.resize(qsize,false); is_accept[qsize-2] = eq; is_accept[qsize-3] = true; } public: LeqADFA(std::string s, bool eq = true, int alpha_size = 10) : str(s), eq(eq) { assert(s.size() >= 1); alphabet_size = alpha_size; initializer(); } }; struct ForbiddenNumberAutomaton : public Automaton { private: std::vector banflg; void initializer() { qsize = 3; init = 0; set_delta(); set_is_accept(); } void set_delta() { delta.resize(qsize,std::vector(alphabet_size)); for (int state = 0; state < qsize; state++) { for (int c = 0; c < alphabet_size; c++) { if (state == 0) { if (c == 0) delta[state][c] = 0; else if (banflg[c]) delta[state][c] = 2; else delta[state][c] = 1; } else if (state == 1) { if (banflg[c]) delta[state][c] = 2; else delta[state][c] = 1; } else { delta[state][c] = 2; } } } } void set_is_accept() { is_accept.resize(qsize,false); is_accept[1] = true; } public: ForbiddenNumberAutomaton(std::vector banflg, int alpha_size = 10) : banflg(banflg) { assert(banflg.size() == alpha_size); alphabet_size = alpha_size; initializer(); } }; Automaton PairADFA(const Automaton &adfa, const Automaton &dfa) { Automaton M; M.alphabet_size = adfa.alphabet_size*dfa.alphabet_size; std::unordered_map table; std::vector x = {adfa.init}, y = {dfa.init}; table[(long long)x[0]*dfa.size()+y[0]] = 0; M.init = 0; for (int i = 0; i < x.size(); ++i) { M.delta.emplace_back(M.alphabet_size, -1); M.is_accept.emplace_back(adfa.accept(x[i]) && dfa.accept(y[i])); for (int c1 = 0; c1 < adfa.alphabet_size; c1++) { for (int c2 = 0; c2 < dfa.alphabet_size; c2++) { int c = c1*dfa.alphabet_size+c2; int u = adfa.next(x[i],c1), v = dfa.next(y[i],c2); long long ps = (long long)u*dfa.size()+v; if (table.find(ps) == table.end()) { table[ps] = x.size(); x.emplace_back(u); y.emplace_back(v); } M.delta[i][c] = table[ps]; } } } M.qsize = M.delta.size(); return M; } struct SameMSDPairAutomaton : public Automaton { private: int alpha_size; void initializer() { qsize = 3; init = 0; set_delta(); set_is_accept(); } void set_delta() { delta.resize(qsize,std::vector(alphabet_size)); for (int c1 = 0; c1 < alpha_size; c1++) { for (int c2 = 0; c2 < alpha_size; c2++) { int c = c1*alpha_size+c2; if (c1 == 0 && c2 == 0) delta[0][c] = 0; else if (c1 == c2) delta[0][c] = 1; else delta[0][c] = 2; } } for (int c = 0; c < alphabet_size; c++) { delta[1][c] = 1; delta[2][c] = 2; } } void set_is_accept() { is_accept.resize(qsize,false); is_accept[0] = is_accept[1] = true; } public: SameMSDPairAutomaton(int alpha_size = 10) : alpha_size(alpha_size) { alphabet_size = alpha_size*alpha_size; initializer(); } }; Automaton IntersectionAutomaton(const Automaton &A, const Automaton &B) { assert(A.alphabet_size == B.alphabet_size); Automaton M; M.alphabet_size = A.alphabet_size; std::vector> table(A.size(), std::vector(B.size(),-1)); std::vector x = {A.init}, y = {B.init}; table[x[0]][y[0]] = 0; M.init = 0; for (int i = 0; i < (int)x.size(); ++i) { M.delta.emplace_back(M.alphabet_size, -1); M.is_accept.emplace_back(A.accept(x[i]) && B.accept(y[i])); for (int c = 0; c < A.alphabet_size; c++) { int u = A.next(x[i],c), v = B.next(y[i],c); if (table[u][v] == -1) { table[u][v] = x.size(); x.emplace_back(u); y.emplace_back(v); } M.delta[i][c] = table[u][v]; } } M.qsize = M.delta.size(); return M; } template Monoid digitDP(const Automaton &adfa) { std::vector indeg(adfa.size()); for (int i = 0; i < adfa.size(); i++) { for (int c = 0; c < adfa.alphabet_size; c++) { indeg[adfa.next(i,c)]++; } } std::vector dp(adfa.size()); dp[adfa.init] = Monoid::e(); Monoid ans; std::queue que; que.push(adfa.init); while(!que.empty()) { int state = que.front(); que.pop(); for (int c = 0; c < adfa.alphabet_size; c++) { int next_s = adfa.next(state,c); dp[next_s] += dp[state]*c; indeg[next_s]--; if (indeg[next_s] == 0) que.push(next_s); } if (adfa.accept(state)) ans += dp[state]; } return ans; } string binarynumber(long long x, int len = -1) { string res; while(x) { if (x&1) res.push_back('1'); else res.push_back('0'); x>>=1; } while((int)res.size() < len) res.push_back('0'); reverse(res.begin(), res.end()); return res; } int main() { ll n;cin >> n; if (n == 0) { puts("0"); return 0; } string ns = binarynumber(n); auto M1 = ForbiddenNumberAutomaton({0,0,0,1},4); for (int i = 0; i < M1.size(); i++) { M1.is_accept[i] = M1.is_accept[i] ^ true; } auto M2 = SameMSDPairAutomaton(2); for (int i = 0; i < M2.size(); i++) { M2.is_accept[i] = M2.is_accept[i] ^ true; } auto M3 = LeqADFA(ns,true,2); auto M4 = PairADFA(M3,M3); auto M5 = IntersectionAutomaton(M1,M2); auto M6 = IntersectionAutomaton(M4,M5); cout << digitDP(M6).val/2 << endl; return 0; }