#include #include using namespace std; constexpr int MOD = 998244353; using i64 = long long; using u64 = unsigned long long; int normalize(i64 value) { value %= MOD; if (value < 0) value += MOD; return int(value); } int modPower(int a, int exponent) { int result = 1; for (; exponent; exponent >>= 1, a = i64(a) * a % MOD) if (exponent & 1) result = i64(result) * a % MOD; return result; } // Arithmetic modulo z^3+z^2+z-1: z^3=1-z-z^2 and z^4=2z-1. struct Element { int a = 0, b = 0, c = 0; }; Element operator+(Element x, Element y) { int a = x.a + y.a, b = x.b + y.b, c = x.c + y.c; if (a >= MOD) a -= MOD; if (b >= MOD) b -= MOD; if (c >= MOD) c -= MOD; return {a, b, c}; } Element operator-(Element x, Element y) { int a = x.a - y.a, b = x.b - y.b, c = x.c - y.c; if (a < 0) a += MOD; if (b < 0) b += MOD; if (c < 0) c += MOD; return {a, b, c}; } Element operator*(Element x, Element y) { i64 t0 = i64(x.a) * y.a; i64 t1 = i64(x.a) * y.b + i64(x.b) * y.a; i64 t2 = i64(x.a) * y.c + i64(x.b) * y.b + i64(x.c) * y.a; i64 t3 = i64(x.b) * y.c + i64(x.c) * y.b; i64 t4 = i64(x.c) * y.c; return {normalize(t0 + t3 - t4), normalize(t1 - t3 + 2 * t4), normalize(t2 - t3)}; } Element square(Element x) { i64 t0 = i64(x.a) * x.a; i64 t1 = 2LL * x.a * x.b; i64 t2 = 2LL * x.a * x.c + i64(x.b) * x.b; i64 t3 = 2LL * x.b * x.c; i64 t4 = i64(x.c) * x.c; return {normalize(t0 + t3 - t4), normalize(t1 - t3 + 2 * t4), normalize(t2 - t3)}; } Element inverse(Element x) { int a = x.a, b = x.b, c = x.c; int q0 = normalize((i64(a) - c) * (a - b) - (2LL * c - b) * (b - c)); int q1 = normalize(i64(c) * (2LL * c - b) - i64(b) * (a - b)); int q2 = normalize(i64(b) * (b - c) - i64(c) * (a - c)); int determinant = normalize(i64(a) * q0 + i64(c) * q1 + (i64(b) - c) * q2); // All requested denominators are units for this modulus and N <= 10^18. assert(determinant != 0); int inv = determinant == 1 ? 1 : modPower(determinant, MOD - 2); return {int(i64(q0) * inv % MOD), int(i64(q1) * inv % MOD), int(i64(q2) * inv % MOD)}; } Element evaluate(Element coefficients, Element z, Element z2) { return {normalize(coefficients.a + i64(coefficients.b) * z.a + i64(coefficients.c) * z2.a), normalize(i64(coefficients.b) * z.b + i64(coefficients.c) * z2.b), normalize(i64(coefficients.b) * z.c + i64(coefficients.c) * z2.c)}; } struct Scale { u64 value; int parent = -1, factor = 0, twice = -1, thrice = -1; Element z, z2, inverseDenominator; }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); u64 N; cin >> N; vector divisors; for (u64 two = 1; two <= N; two *= 2) for (u64 d = two; d <= N; d *= 3) divisors.push_back(d); sort(divisors.begin(), divisors.end()); int count = int(divisors.size()); auto scaleIndex = [&](u64 d) { return int(lower_bound(divisors.begin(), divisors.end(), d) - divisors.begin()); }; const Element ONE{1, 0, 0}, Z{0, 1, 0}; vector scales(count); for (int j = 0; j < count; ++j) { Scale& scale = scales[j]; scale.value = divisors[j]; if (j == 0) { scale.z = Z; } else { scale.factor = divisors[j] % 2 == 0 ? 2 : 3; scale.parent = scaleIndex(divisors[j] / scale.factor); Element parent = scales[scale.parent].z; scale.z = square(parent); if (scale.factor == 3) scale.z = scale.z * parent; } scale.z2 = square(scale.z); if (j) scale.inverseDenominator = inverse(ONE - scale.z - scale.z2 - scale.z2 * scale.z); if (divisors[j] <= N / 2) scale.twice = scaleIndex(divisors[j] * 2); if (divisors[j] <= N / 3) scale.thrice = scaleIndex(divisors[j] * 3); } vector sizes; for (u64 d : divisors) sizes.push_back(N / d); sort(sizes.begin(), sizes.end()); sizes.erase(unique(sizes.begin(), sizes.end()), sizes.end()); int rows = int(sizes.size()); vector offset(rows + 1), length(rows), half(rows), third(rows); auto rowIndex = [&](u64 size) { return size == 0 ? -1 : int(lower_bound(sizes.begin(), sizes.end(), size) - sizes.begin()); }; for (int i = 0; i < rows; ++i) { length[i] = int(upper_bound(divisors.begin(), divisors.end(), N / sizes[i]) - divisors.begin()); offset[i + 1] = offset[i] + length[i]; half[i] = rowIndex(sizes[i] / 2); third[i] = rowIndex(sizes[i] / 3); } // prefix[offset[i]+j] = sum_(k=1)^sizes[i] f(k) z^(divisors[j]*k). // Scale 1 is singular and is never used as a prefix-sum state. vector prefix(offset.back()); array binaryPower; binaryPower[0] = Z; for (int i = 1; i < 64; ++i) binaryPower[i] = square(binaryPower[i - 1]); auto powerZ = [&](u64 exponent) { Element result = ONE; for (int bit = 0; exponent; exponent >>= 1, ++bit) if (exponent & 1) result = result * binaryPower[bit]; return result; }; auto get = [&](int row, int scale) -> Element { if (row < 0) return {}; assert(0 <= scale && scale < length[row]); return prefix[offset[row] + scale]; }; int answer = 0; for (int i = 0; i < rows; ++i) { vector powers(length[i]); powers[0] = powerZ(sizes[i] + 1); Element boundary = inverse(powers[0]) * (Z + get(half[i], scales[0].twice) + get(third[i], scales[0].thrice)); // boundary = (f(n)+f(n-1)+f(n-2)) + // (f(n)+f(n-1))*z + f(n)*z^2. answer = boundary.c; for (int j = 1; j < length[i]; ++j) { const Scale& scale = scales[j]; Element parentPower = powers[scale.parent]; powers[j] = square(parentPower); if (scale.factor == 3) powers[j] = powers[j] * parentPower; Element numerator = scale.z + get(half[i], scale.twice) + get(third[i], scale.thrice) - powers[j] * evaluate(boundary, scale.z, scale.z2); prefix[offset[i] + j] = numerator * scale.inverseDenominator; } } cout << answer << '\n'; }