結果

問題 No.3505 Sum of Prod of Root
コンテスト
ユーザー ryoku
提出日時 2026-04-18 11:11:03
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 326 ms / 2,000 ms
コード長 24,514 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,919 ms
コンパイル使用メモリ 314,736 KB
実行使用メモリ 7,808 KB
最終ジャッジ日時 2026-04-18 11:11:11
合計ジャッジ時間 6,877 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 13
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#line 1 "a.cpp"
#line 1 "a.cpp"
#line 2 "lib/template.hpp"
# pragma GCC optimize("unroll-loops")

#pragma GCC optimize("O3")
using namespace std;
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <climits>
#include <cassert>
#include <functional>
#include <iterator>
#include <utility>
#include <complex>
#include <bitset>
#include <chrono>
#include <random>
#include <limits>
#include <optional>
#include <variant>
#include <any>

#include <array>
#include <bit>
#include <compare>
#include <concepts>
#include <format>
#include <numbers>
#include <ranges>
#include <span>
#include <string_view>
#include <tuple>
#include <type_traits>
#include <version>
using uint=unsigned;
using ll=long long;
using ull=unsigned long long;
using ld=long double;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
using i128=__int128;
template<class T>using vc=vector<T>;
template<class T>using vvc=vc<vc<T>>;
template<class T>using vvvc=vvc<vc<T>>;
#define vec(d,T,n,...) vec_##d(T,n,__VA_ARGS__)
#define vec_1(T,n,a) vector<T> n(a)
#define vec_2(T,n,a,b) vector<vector<T>> n(a,vector<T>(b))
#define vec_3(T,n,a,b,c) vector<vector<vector<T>>> n(a,vector<vector<T>>(b,vector<T>(c)))
#define vec_4(T,n,a,b,c,d) vector<vector<vector<vector<T>>>> n(a,vector<vector<vector<T>>>(b,vector<vector<T>>(c,vector<T>(d))))
#define vec_5(T,n,a,b,c,d,e) vector<vector<vector<vector<vector<T>>>>> n(a,vector<vector<vector<vector<T>>>>(b,vector<vector<vector<T>>>(c,vector<vector<T>>(d,vector<T>(e)))))
#define vec_6(T,n,a,b,c,d,e,f) vector<vector<vector<vector<vector<vector<T>>>>>> n(a,vector<vector<vector<vector<vector<T>>>>>(b,vector<vector<vector<vector<T>>>>(c,vector<vector<vector<T>>>(d,vector<vector<T>>(e,vector<T>(f))))))
#define vec_7(T,n,a,b,c,d,e,f,g) vector<vector<vector<vector<vector<vector<vector<T>>>>>>> n(a,vector<vector<vector<vector<vector<vector<T>>>>>>(b,vector<vector<vector<vector<vector<T>>>>>(c,vector<vector<vector<vector<T>>>>(d,vector<vector<vector<T>>>(e,vector<vector<T>>(f,vector<T>(g)))))))
template<class T>using smpq=priority_queue<T,vector<T>,greater<T>>;
template<class T>using bipq=priority_queue<T>;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define REP(i,j,n) for(ll i=(j);i<(ll)(n);i++)
#define DREP(i,n,m) for(ll i=(n);i>=(m);i--)
#define drep(i,n) for(ll i=((n)-1);i>=0;i--)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define is insert
#define bg begin()
#define ed end()
void scan(int&a) { cin >> a; }
void scan(ll&a) { cin >> a; }
void scan(string&a) { cin >> a; }
void scan(char&a) { cin >> a; }
void scan(uint&a) { cin >> a; }
void scan(ull&a) { cin >> a; }
void scan(bool&a) { cin >> a; }
void scan(ld&a){ cin>> a;}
template<class T> void scan(vector<T>&a) { for(auto&x:a) scan(x); }
void read() {}
template<class Head, class... Tail> void read(Head&head, Tail&... tail) { scan(head); read(tail...); }
#define INT(...) int __VA_ARGS__; read(__VA_ARGS__);
#define LL(...) ll __VA_ARGS__; read(__VA_ARGS__);
#define ULL(...) ull __VA_ARGS__; read(__VA_ARGS__);
#define STR(...) string __VA_ARGS__; read(__VA_ARGS__);
#define CHR(...) char __VA_ARGS__; read(__VA_ARGS__);
#define DBL(...) double __VA_ARGS__; read(__VA_ARGS__);
#define LD(...) ld __VA_ARGS__; read(__VA_ARGS__);
#define VC(type, name, ...) vector<type> name(__VA_ARGS__); read(name);
#define VVC(type, name, size, ...) vector<vector<type>> name(size, vector<type>(__VA_ARGS__)); read(name);
template<class T>void print(T a) { cout << a; }
template<class T> void print(vector<T>a) { for(int i=0;i<(int)a.size();i++){if(i)cout<<" ";print(a[i]);}cout<<endl;}
void PRT() { cout <<endl; return ; }
template<class T> void PRT(T a) { print(a); cout <<endl; return; }
template<class Head, class... Tail> void PRT(Head head, Tail ... tail) { print(head); cout << " "; PRT(tail...); return; }
template<class T,class F>
bool chmin(T &x, F y){
    if(x>y){
        x=y;
        return true;
    }
    return false;
}
template<class T, class F>
bool chmax(T &x, F y){
    if(x<y){
        x=y;
        return true;
    }
    return false;
}
template <typename T>
T floor(T a, T b) {
  return a / b - (a % b && (a ^ b) < 0);
}
template <typename T>
T ceil(T x, T y) {
  return floor(x + y - 1, y);
}
template <typename T>
T bmod(T x, T y) {
  return x - y * floor(x, y);
}
template <typename T>
pair<T, T> divmod(T x, T y) {
  T q = floor(x, y);
  return {q, x - q * y};
}
void YesNo(bool b){
    cout<<(b?"Yes":"No")<<endl;
}
void Yes(){
    cout<<"Yes"<<endl;
}
void No(){
    cout<<"No"<<endl;
}
template<class T=ll>
T isqrt(T x){
    T F=sqrtl(x);
    while((F+1)*(F+1)<=x)F++;
    while(F*F>x)F--;
    return F;
}
template<class T>
int popcount(T n){
    return __builtin_popcountll(n);
}
template<class T,class L=ll>
L sum(vc<T>&a){
    return accumulate(all(a),L(0));
}
template<class T>
vc<T>subset(T S){
    vc<T>ans;
    for(T x=S;x>0;x=(x-1)&S)ans.pb(x);
    ans.pb(0);
    return ans;
}
template<class T>
T max(vc<T>&a){
    return *max_element(all(a));
}
template<class T>
T min(vc<T>&a){
    return *min_element(all(a));
}
vvc<int>readgraph(int n,int m,int off = -1){
    vvc<int> g(n);
    rep(i, m){
        int u,v;
        cin>>u>>v;
        u+=off,v+=off;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    return g;
}
vvc<int> readtree(int n,int off=-1){
    return readgraph(n,n-1,off);
}
template<class T,class L=ll>
vc<L> presum(vc<T> &a){
    vc<L> ret(a.size()+1);
    rep(i,a.size())ret[i+1]=ret[i]+a[i];
    return ret;
}
template<class T, class F>
vc<T> &operator+=(vc<T> &a,F b){
    for (auto&v:a)v += b;
    return a;
}
template<class T, class F>
vc<T> &operator-=(vc<T>&a,F b){
    for (auto&v:a)v-=b;
    return a;
}
template<class T, class F>
vc<T> &operator*=(vc<T>&a,F b){
    for (auto&v:a)v*=b;
    return a;
}
template<class T=ll>
constexpr T POW(T a,T b){
    T res=1;
    while(b){
        if(b&1)res*=a;
        a*=a;
        b/=2;
    }
    return res;
}
constexpr ll ten(ll a){
    return POW<ll>(10,a);
}
template<class T>
struct Compress{    
    vc<T>v;
    bool worked;
    Compress(){worked=0;}
    Compress(int n){v.reserve(n);worked=0;}
    void push(T x){
        v.push_back(x);
    }
    void work(){
        if(worked)return;
        worked=1;
        sort(all(v));
        v.erase(unique(all(v)),v.end());
    }
    int find(T x){
        work();
        auto itr=lower_bound(all(v),x);
        if((*itr)==x)return itr-v.begin();
        return -1;
    }
    int find_next(T x){
        work();
        return lower_bound(all(v),x)-v.begin();
    }
    int find_prev(T x){
        work();
        return lower_bound(all(v),x)-v.begin()-1;
    }
};
int tbit(int32_t x){return x==0?-1:31-__builtin_clz((uint32_t)x);}
int lbit(int32_t x){return x==0?-1:__builtin_ctz((uint32_t)x);}
int tbit(uint32_t x){return x==0?-1:31-__builtin_clz(x);}
int lbit(uint32_t x){return x==0?-1:__builtin_ctz(x);}
int tbit(int64_t x){return x==0?-1:63-__builtin_clzll((uint64_t)x);}
int lbit(int64_t x){return x==0?-1:__builtin_ctzll((uint64_t)x);}
int tbit(uint64_t x){return x==0?-1:63-__builtin_clzll(x);}
int lbit(uint64_t x){return x==0?-1:__builtin_ctzll(x);}
int tbit(int32_t x,int p){return p<0?-1:tbit((int32_t)(x&(p>=31?~0u:(1u<<(p+1))-1)));}
int lbit(int32_t x,int p){return p>31?-1:lbit((int32_t)(x&(~0u<<p)));}
int tbit(uint32_t x,int p){return p<0?-1:tbit((uint32_t)(x&(p>=31?~0u:(1u<<(p+1))-1)));}
int lbit(uint32_t x,int p){return p>31?-1:lbit((uint32_t)(x&(~0u<<p)));}
int tbit(int64_t x,int p){return p<0?-1:tbit((int64_t)(x&(p>=63?~0ULL:(1ULL<<(p+1))-1)));}
int lbit(int64_t x,int p){return p>63?-1:lbit((int64_t)(x&(~0ULL<<p)));}
int tbit(uint64_t x,int p){return p<0?-1:tbit((uint64_t)(x&(p>=63?~0ULL:(1ULL<<(p+1))-1)));}
int lbit(uint64_t x,int p){return p>63?-1:lbit((uint64_t)(x&(~0ULL<<p)));}
std::istream& operator>>(std::istream& is, __int128& x) {
    std::streambuf* sb = is.rdbuf();

    int c = sb->sgetc();
    while (c <= ' ') c = sb->snextc();

    bool neg = false;
    if (c == '-') {
        neg = true;
        c = sb->snextc();
    }

    unsigned __int128 v = 0;

    while ((unsigned)(c - '0') < 10) {
        v = v * 10 + (c - '0');
        c = sb->snextc();
    }

    x = neg ? -(__int128)v : (__int128)v;
    return is;
}

std::ostream& operator<<(std::ostream& os, __int128 x) {
    std::streambuf* sb = os.rdbuf();

    if (x == 0) {
        sb->sputc('0');
        return os;
    }

    char buf[40];
    int pos = 39;

    bool neg = x < 0;
    unsigned __int128 v = neg ? -(unsigned __int128)x : (unsigned __int128)x;

    while (v) {
        buf[--pos] = char('0' + (v % 10));
        v /= 10;
    }

    if (neg) buf[--pos] = '-';

    sb->sputn(buf + pos, 39 - pos);
    return os;
}
#ifdef LOCAL

template<typename T, typename U> std::ostream& operator<<(std::ostream& os, const std::pair<T, U>& p);
template<typename T> std::ostream& operator<<(std::ostream& os, const std::vector<T>& v);
template<typename T> std::ostream& operator<<(std::ostream& os, const std::deque<T>& dq);
template<typename T> std::ostream& operator<<(std::ostream& os, const std::list<T>& lst);
template<typename T> std::ostream& operator<<(std::ostream& os, const std::set<T>& s);
template<typename T> std::ostream& operator<<(std::ostream& os, const std::unordered_set<T>& s);
template<typename K, typename V> std::ostream& operator<<(std::ostream& os, const std::map<K, V>& m);
template<typename K, typename V> std::ostream& operator<<(std::ostream& os, const std::unordered_map<K, V>& m);
template<typename T> std::ostream& operator<<(std::ostream& os, std::stack<T> st);
template<typename T> std::ostream& operator<<(std::ostream& os, std::queue<T> q);
template<typename T, std::size_t N> std::ostream& operator<<(std::ostream& os, const std::array<T, N>& a);
template <typename T, typename Container, typename Compare>
std::ostream& operator<<(std::ostream& os, std::priority_queue<T, Container, Compare> pq);

#define dbg(...) debug_func(0, #__VA_ARGS__, __VA_ARGS__)

template <typename T>
void debug_func(int i, T name) {
  cout << endl;
}

template <typename T1, typename T2, typename... T3>
void debug_func(int i, const T1 &name, const T2 &a, const T3 &...b) {
  for ( ; name[i] != ',' && name[i] != '\0'; i++ ) cout << name[i];
  cout << ":" << a << " ";
  debug_func(i + 1, name, b...);
}
// pair
template<typename T, typename U>
std::ostream& operator<<(std::ostream& os, const std::pair<T, U>& p) {
    return os << "(" << p.first << ", " << p.second << ")";
}
// vector
template<typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
    os << "[";
    for (size_t i = 0; i < v.size(); ++i) {
        if (i > 0) os << ", ";
        os << v[i];
    }
    os << "]";
    return os;
}

template<typename T>
std::ostream& operator<<(std::ostream& os, const std::deque<T>& dq) {
    os << "[";
    for (size_t i = 0; i < dq.size(); ++i) {
        if (i > 0) os << ", ";
        os << dq[i];
    }
    os << "]";
    return os;
}

template<typename T>
std::ostream& operator<<(std::ostream& os, const std::list<T>& lst) {
    os << "[";
    bool first = true;
    for (const auto& x : lst) {
        if (!first) os << ", ";
        os << x;
        first = false;
    }
    os << "]";
    return os;
}

template<typename T>
std::ostream& operator<<(std::ostream& os, const std::set<T>& s) {
    os << "{";
    bool first = true;
    for (const auto& x : s) {
        if (!first) os << ", ";
        os << x;
        first = false;
    }
    os << "}";
    return os;
}

template<typename T>
std::ostream& operator<<(std::ostream& os, const std::unordered_set<T>& s) {
    os << "{";
    bool first = true;
    for (const auto& x : s) {
        if (!first) os << ", ";
        os << x;
        first = false;
    }
    os << "}";
    return os;
}

template<typename K, typename V>
std::ostream& operator<<(std::ostream& os, const std::map<K, V>& m) {
    os << "{";
    bool first = true;
    for (const auto& kv : m) {
        if (!first) os << ", ";
        os << kv;
        first = false;
    }
    os << "}";
    return os;
}

template<typename K, typename V>
std::ostream& operator<<(std::ostream& os, const std::unordered_map<K, V>& m) {
    os << "{";
    bool first = true;
    for (const auto& kv : m) {
        if (!first) os << ", ";
        os << kv;
        first = false;
    }
    os << "}";
    return os;
}

template<typename T>
std::ostream& operator<<(std::ostream& os, std::stack<T> st) {
    os << "[";
    bool first = true;
    while (!st.empty()) {
        if (!first) os << ", ";
        os << st.top();
        st.pop();
        first = false;
    }
    os << "]";
    return os;
}

template<typename T>
std::ostream& operator<<(std::ostream& os, std::queue<T> q) {
    os << "[";
    bool first = true;
    while (!q.empty()) {
        if (!first) os << ", ";
        os << q.front();
        q.pop();
        first = false;
    }
    os << "]";
    return os;
}

template<typename T, typename Container, typename Compare>
std::ostream& operator<<(std::ostream& os, std::priority_queue<T, Container, Compare> pq) {
    os << "[";
    while (!pq.empty()) {
        os << pq.top() << (pq.size() > 1 ? ", " : "");
        pq.pop();
    }
    return os << "]";
}
template<typename T, std::size_t N>
std::ostream& operator<<(std::ostream& os, const std::array<T, N>& a) {
    os << "[";
    for (std::size_t i = 0; i < N; ++i) {
        if (i > 0) os << ", ";
        os << a[i];
    }
    os << "]";
    return os;
}
#else
#define dbg(...) 1111
#endif
#line 3 "lib/math/barrett.hpp"
struct Barrett{
    using u64=uint64_t;
    using u32=uint32_t;
    using i128=__int128_t;
    u64 m;
    int mod;
    void set(int mod_){
        mod=mod_;
        m=(i128(1)<<64)/mod;
    }
    unsigned reduce(uint64_t x){
        x-=(((i128)x*m)>>64)*mod;
        return x<mod?x:x-mod;
    }
};
#line 4 "lib/math/d_mint.hpp"
template<int id>
struct DynamicModInt{
    using u32=uint32_t;
    using u64=uint64_t;
    u32 val;
    DynamicModInt():val(0){}
    DynamicModInt(ll x){
        int v=x%get_mod();
        if(v<0)v+=get_mod();
        val=v;
    }
    static DynamicModInt raw(int v){
        assert(v>=0);
        DynamicModInt mi;
        mi.val=v;
        return mi;
    }
    DynamicModInt &operator+=(const DynamicModInt&m){
        if((val+=m.val)>=get_mod())val-=get_mod();
        return *this;
    }
    DynamicModInt &operator-=(const DynamicModInt&m){
        if((val+=(get_mod()-m.val))>=get_mod())val-=get_mod();
        return *this;
    }
    DynamicModInt &operator*=(const DynamicModInt&m){
        val=rem(u64(val)*m.val);
        return *this;
    }
    DynamicModInt &operator/=(const DynamicModInt&m){
        val=rem(u64(val)*m.inv().val);
        return *this;
    }
    DynamicModInt operator-() const{
        return DynamicModInt(-val);
    }
    DynamicModInt operator+() const {
        return *this;
    }
    friend DynamicModInt operator+(DynamicModInt lhs, const DynamicModInt& rhs){
        return lhs+=rhs;
    }
    friend DynamicModInt operator-(DynamicModInt lhs, const DynamicModInt& rhs){
        return lhs-=rhs;
    }
    friend DynamicModInt operator*(DynamicModInt lhs, const DynamicModInt& rhs){
        return lhs*=rhs;
    }
    friend DynamicModInt operator/(DynamicModInt lhs,const DynamicModInt&rhs){
        return lhs/=rhs;
    }
    bool operator==(const DynamicModInt&p) const{
        return p.val==val;
    }
    bool operator!=(const DynamicModInt&p) const{
        return p.val!=val;
    }
    DynamicModInt pow(int64_t n) const{
        DynamicModInt res(1),mul(val);
        while(n){
            if(n%2)res*=mul;
            mul*=mul;
            n/=2;
        }
        return res;
    }

    friend ostream&operator<<(ostream&os,const DynamicModInt&p){
        os<<p.val;
        return os;
    }
    friend istream&operator>>(istream&is,DynamicModInt&p){
        int64_t x;
        is>>x;
        p=DynamicModInt(x);
        return is;
    }
    DynamicModInt inv()const{
        int a=val,b=get_mod(),u=1,v=0,t;
        #ifdef LOCAL
        assert(gcd(a,b)==1);
        #endif
        while(b>0){
            t=a/b;
            swap(a-=t*b,b);
            swap(u-=t*v,v);
        }
        return DynamicModInt(u);
    }
    inline static u32 rem(u64 x){return BarrettReduction().reduce(x);}
    static inline int &get_mod(){
        static int mod=0;
        return mod;
    }
    static void set_mod(int md){
        assert(0<md&&md<=(1ll<<30)-1);
        get_mod()=md;
        BarrettReduction().set(md);
    }
    static inline Barrett&BarrettReduction(){
        static Barrett b;
        return b;
    }
};

#line 4 "lib/math/mod.hpp"
template<class mint>
struct Binom{
    inline static vector<mint>fact={1},invfact={1};
    static void build(int n){
        if(n<(int)fact.size())return;
        fact=invfact=vector<mint>(n+1);
        fact[0]=1;
        for(int i=1;i<=n;i++)fact[i]=fact[i-1]*i;
        invfact[n]=(1/fact[n]);
        for(int i=n-1;i>=0;i--)invfact[i]=invfact[i+1]*(i+1);
    }
    static mint C(int a,int b){//aCb
        if(a<0||b<0||a-b<0)return mint(0);
        while((int)fact.size()<=a){
            fact.push_back(fact.back()*(fact.size()));
        }
        while((int)invfact.size()<=a){
            invfact.push_back(invfact.back()/invfact.size());
        }
        return fact[a]*invfact[b]*invfact[a-b];
    }
    static mint P(int a,int b){
        if(a<b||b<0)return 0;
        return fact[a]*invfact[a-b];
    }
    static mint H(int a,int b){
        return C(a+b-1,b);
    }  
};
template<class T>
pair<T,T> inv(T x,T m){
    T a1,a2;
    T res=extgcd(x,m,a1,a2);
    return {a1,m/res};
}
template<class T>
pair<T,T> mod_solve(T a,T b,T m){//return x s.t. ax=b mod m
    a%=m,b%=m;if(a<0)a+=m;if(b<0)b+=m;
    T g=gcd(gcd(a,b),m);
    a/=g,b/=g,m/=g;
    if(gcd(a,m)>1)return {-1,-1};
    return {(inv(a,m).first*b)%m,inv(a,m).second};
}
//https://nyaannyaan.github.io/library/modulo/mod-sqrt.hpp.html
int64_t mod_sqrt(const int64_t &a, const int64_t &p) {
  assert(0 <= a && a < p);
  if (a < 2) return a;
  using Mint = DynamicModInt<409075245>;
  Mint::set_mod(p);
  if (Mint(a).pow((p - 1) >> 1) != 1) return -1;
  Mint b = 1, one = 1;
  while (b.pow((p - 1) >> 1) == 1) b += one;
  int64_t m = p - 1, e = 0;
  while (m % 2 == 0) m >>= 1, e += 1;
  Mint x = Mint(a).pow((m - 1) >> 1);
  Mint y = Mint(a) * x * x;
  x *= a;
  Mint z = Mint(b).pow(m);
  while (y != 1) {
    int64_t j = 0;
    Mint t = y;
    while (t != one) {
      j += 1;
      t *= t;
    }
    z = z.pow(int64_t(1) << (e - j - 1));
    x *= z;
    z *= z;
    y *= z;
    e = j;
  }
  return x.val;
}
#line 4 "lib/math/s_mint.hpp"
template<uint32_t mod>
struct StaticModInt{
    using u32=uint32_t;
    using u64=uint64_t;
    u32 val;
    StaticModInt():val(0){}
    StaticModInt(ll x){
        int v=x%mod;
        if(v<0)v+=mod;
        val=v;
    }
    constexpr static uint32_t get_mod(){
        return mod;
    }
    static StaticModInt raw(int v){
        assert(v>=0);
        StaticModInt mi;
        mi.val=v;
        return mi;
    }
    StaticModInt &operator+=(const StaticModInt&m){
        if((val+=m.val)>=mod)val-=mod;
        return *this;
    }
    StaticModInt &operator-=(const StaticModInt&m){
        if((val+=(mod-m.val))>=mod)val-=mod;
        return *this;
    }
    StaticModInt &operator*=(const StaticModInt&m){
        val=u64(val)*m.val%mod;
        return *this;
    }
    StaticModInt &operator/=(const StaticModInt&m){
        val=u64(val)*m.inv().val%mod;
        return *this;
    }
    StaticModInt operator-() const{
        return StaticModInt(mod-val);
    }
    StaticModInt operator+() const {
        return *this;
    }
    friend StaticModInt operator+(StaticModInt lhs, const StaticModInt& rhs){
        return lhs+=rhs;
    }
    friend StaticModInt operator-(StaticModInt lhs, const StaticModInt& rhs){
        return lhs-=rhs;
    }
    friend StaticModInt operator*(StaticModInt lhs, const StaticModInt& rhs){
        return lhs*=rhs;
    }
    friend StaticModInt operator/(StaticModInt lhs,const StaticModInt&rhs){
        return lhs/=rhs;
    }
    bool operator==(const StaticModInt&p) const{
        return p.val==val;
    }
    bool operator!=(const StaticModInt&p) const{
        return p.val!=val;
    }
    StaticModInt pow(int64_t n) const{
        StaticModInt res(1),mul(val);
        while(n){
            if(n%2)res*=mul;
            mul*=mul;
            n/=2;
        }
        return res;
    }

    friend ostream&operator<<(ostream&os,const StaticModInt&p){
        os<<p.val;
        return os;
    }
    friend istream&operator>>(istream&is,StaticModInt&p){
        int64_t x;
        is>>x;
        p=StaticModInt(x);
        return is;
    }
    StaticModInt inv()const{
        int a=val,b=mod,u=1,v=0,t;
        #ifdef LOCAL
        assert(gcd(a,b)==1);
        #endif
        while(b>0){
            t=a/b;
            swap(a-=t*b,b);
            swap(u-=t*v,v);
        }
        return StaticModInt(u);
    }
};

#line 3 "lib/math/kth_root.hpp"
//floor(a^{1/b})
ull kth_root(ull a,ull b){
    if(a==0)return 0;
    if(b==1)return a;
    if(b>=64)return 1;
    auto my_pow=[&](ull A,ull B)->ull{
        ull res=1;
        while(B){
            if(B%2){
                if(i128(res)*A>a)return 0;
                res*=A;
            }
            B/=2;
            if(B){
                if(i128(A)*A>a)return 0;
                A*=A;
            }
        }
        return res;
    };
    ull ac=1,wa=(ull(1)<<(64/b+1))+1;
    while(wa-ac>1){
        ull wj=ac+wa>>1;
        auto res=my_pow(wj,b);
        if(res&&res<=a){
            ac=wj;
        }else wa=wj;
    }
    return ac;
}
#line 4 "a.cpp"
using mint=StaticModInt<998244353>;
mint inv2=499122177;
mint i20=149736653;
inline mint sm(mint l){
    return mint(l)*(l-1)*inv2;
}
int cnt=0;
inline mint get_sum(ll l,ll r){
    ll li=sqrt(l);if(li*li>l)li--;
    ll ri=sqrt(r);if(ri*ri>r)ri--;
    mint ans=0;
    if(li==ri){
        ans+=mint(li)*(sm(r+1)-sm(l));
        return ans;
    }
    ans+=mint(li)*(sm((li+1)*(li+1))-sm(l));
    ans+=mint(ri)*(sm(r+1)-sm(ri*ri));
    mint L1=(li+1);
    mint R1=ri;
    ans+=L1*(sm(R1*R1)-sm(L1*L1));
    //REP(i,L1.val+1,R1.val+1)ans+=(sm(R1*R1)-sm(i*i))*coef;
    auto L2=L1*L1;
    auto L3=L1*L2;
    auto L4=L1*L3;
    auto L5=L1*L4;
    auto R2=R1*R1;
    auto R3=R1*R2;
    auto R4=R1*R3;
    auto R5=R1*R4;
    ans+=(2*L5+5*L4-5*L2-2*L1*(5*R4-5*R2+1)+R1*(8*R4-5*R3-10*R2+5*R1+2))*i20;
    return ans;
}
void solve(){
    LL(n);
    vc<mint>inv(1e6);REP(i,1,1e6)inv[i]=1/mint(i);
    mint ans=0;
    vvc<ll>put(63);
    REP(i,4,63){
        for(int j=1;;j++){
            i128 P=POW<i128>(j,i);
            if(P>n)break;
            put[i].push_back(P);
        }
    }
    smpq<array<ll,3>>que;REP(i,4,63){
      if(put[i].size()>1)que.push({put[i][1],i,1});
      cnt+=put[i].size();
    }
    vc<ll>coef(63,1);
    for(ll K=1;K<=1e6;K++){
        ll L=ll(K)*K*K;
        ll R=min(n+1,ll(K+1)*(K+1)*(K+1));
        
        if(L>R)break;
        mint all_coef=K;REP(i,4,63)all_coef*=coef[i];
        //[L,R)
        array<ll,3>tmp{-1,-1,-1};
        auto get_top=[&](){
            return que.top();
        };  
        while(que.size()&&get_top()[0]<R){
            auto [p,q,r]=que.top();que.pop();tmp={-1,-1,-1};
            ans+=get_sum(L,p-1)*all_coef;
            L=p;
            auto update=[&](int p,int q,int r){
                all_coef*=inv[coef[q]++];
                all_coef*=coef[q];
                ++cnt;
                if(put[q].size()>r+1)que.push({put[q][r+1],q,r+1});
            };
            update(p,q,r);
            while(que.size()&&get_top()[0]==p){
                auto[np,nq,nr]=que.top();que.pop();tmp={-1,-1,-1};
                ++cnt;
                update(np,nq,nr);
            }
        }
        ans+=get_sum(L,R-1)*all_coef;
    }
    PRT(ans);
}
signed main(){
    dbg("==============="s);
    #ifdef LOCAL
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    cin.tie(0)->sync_with_stdio(0);
    cout<<fixed<<setprecision(20);
    int t=1;
    // cin >> t;
    while(t--)solve();
}
0