#include #include #include #include #include #include namespace nachia{ template class CsrArray{ public: struct ListRange{ using iterator = typename std::vector::iterator; iterator begi, endi; iterator begin() const { return begi; } iterator end() const { return endi; } int size() const { return (int)std::distance(begi, endi); } Elem& operator[](int i) const { return begi[i]; } }; struct ConstListRange{ using iterator = typename std::vector::const_iterator; iterator begi, endi; iterator begin() const { return begi; } iterator end() const { return endi; } int size() const { return (int)std::distance(begi, endi); } const Elem& operator[](int i) const { return begi[i]; } }; private: int m_n; std::vector m_list; std::vector m_pos; public: CsrArray() : m_n(0), m_list(), m_pos() {} static CsrArray Construct(int n, std::vector> items){ CsrArray res; res.m_n = n; std::vector buf(n+1, 0); for(auto& [u,v] : items){ ++buf[u]; } for(int i=1; i<=n; i++) buf[i] += buf[i-1]; res.m_list.resize(buf[n]); for(int i=(int)items.size()-1; i>=0; i--){ res.m_list[--buf[items[i].first]] = std::move(items[i].second); } res.m_pos = std::move(buf); return res; } static CsrArray FromRaw(std::vector list, std::vector pos){ CsrArray res; res.m_n = pos.size() - 1; res.m_list = std::move(list); res.m_pos = std::move(pos); return res; } ListRange operator[](int u) { return ListRange{ m_list.begin() + m_pos[u], m_list.begin() + m_pos[u+1] }; } ConstListRange operator[](int u) const { return ConstListRange{ m_list.begin() + m_pos[u], m_list.begin() + m_pos[u+1] }; } int size() const { return m_n; } int fullSize() const { return (int)m_list.size(); } }; } // namespace nachia namespace nachia{ struct Graph { public: struct Edge{ int from, to; void reverse(){ std::swap(from, to); } int xorval() const { return from ^ to; } }; Graph(int n = 0, bool undirected = false, int m = 0) : m_n(n), m_e(m), m_isUndir(undirected) {} Graph(int n, const std::vector>& edges, int undirected = false) : m_n(n), m_isUndir(undirected){ m_e.resize(edges.size()); for(std::size_t i=0; i static Graph Input(Cin& cin, int n, bool undirected, int m, int offset = 0){ Graph res(n, undirected, m); for(int i=0; i> u >> v; res[i].from = u - offset; res[i].to = v - offset; } return res; } int numVertices() const noexcept { return m_n; } int numEdges() const noexcept { return int(m_e.size()); } int addNode() noexcept { return m_n++; } int addEdge(int from, int to){ m_e.push_back({ from, to }); return numEdges() - 1; } Edge& operator[](int ei) noexcept { return m_e[ei]; } const Edge& operator[](int ei) const noexcept { return m_e[ei]; } Edge& at(int ei) { return m_e.at(ei); } const Edge& at(int ei) const { return m_e.at(ei); } auto begin(){ return m_e.begin(); } auto end(){ return m_e.end(); } auto begin() const { return m_e.begin(); } auto end() const { return m_e.end(); } bool isUndirected() const noexcept { return m_isUndir; } void reverseEdges() noexcept { for(auto& e : m_e) e.reverse(); } void contract(int newV, const std::vector& mapping){ assert(numVertices() == int(mapping.size())); for(int i=0; i induce(int num, const std::vector& mapping) const { int n = numVertices(); assert(n == int(mapping.size())); for(int i=0; i indexV(n), newV(num); for(int i=0; i= 0) indexV[i] = newV[mapping[i]]++; std::vector res; res.reserve(num); for(int i=0; i= 0) res[mapping[e.to]].addEdge(indexV[e.from], indexV[e.to]); return res; } CsrArray getEdgeIndexArray(bool undirected) const { std::vector> src; src.reserve(numEdges() * (undirected ? 2 : 1)); for(int i=0; i::Construct(numVertices(), src); } CsrArray getEdgeIndexArray() const { return getEdgeIndexArray(isUndirected()); } CsrArray getAdjacencyArray(bool undirected) const { std::vector> src; src.reserve(numEdges() * (undirected ? 2 : 1)); for(auto e : m_e){ src.emplace_back(e.from, e.to); if(undirected) src.emplace_back(e.to, e.from); } return CsrArray::Construct(numVertices(), src); } CsrArray getAdjacencyArray() const { return getAdjacencyArray(isUndirected()); } private: int m_n; std::vector m_e; bool m_isUndir; }; } // namespace nachia namespace nachia{ // simple graph // for each edge // O( n + m sqrt(m) ) time template std::vector CountC4Simple( int n, Graph g, std::vector W ){ int m = int(W.size()); // less incident edges, smaller index std::vector deg(n); for(auto [u,v] : g){ deg[u]++; deg[v]++; } std::vector I(n); for(int i=0; i O(n); for(int i=0; i estart(n); for(int i=0; i eend = estart; std::vector eid(m*2); std::vector eto(m*2); for(int e=0; e eendx = eend; for(int v=0; v c(n); // c[x] : number of paths(v --> w --> x) std::vector ans(m); for(int v=n-1; v>=0; v--){ for(int i=estart[v]; i v for(int j=estart[w]; j std::vector CountC4( int n, Graph g, std::vector W ){ int m = int(W.size()); for(auto& e : g) if(e.to < e.from) e.reverse(); std::vector I(m); for(int i=0; i Q(m); Graph g2; int g2sz = 0; std::vector W2; for(auto e : I){ if(g2sz == 0 || g2[g2sz-1].from != g[e].from || g2[g2sz-1].to != g[e].to){ g2.addEdge(g[e].from, g[e].to); W2.push_back(0); g2sz++; } W2.back() += W[e]; Q[e] = g2sz-1; } auto simple_res = CountC4Simple(n, std::move(g2), std::move(W2)); std::vector ans(m); for(int e=0; e void EnumerateTriangles(Graph G, F query){ int n = G.numVertices(); std::vector C(n); for(auto e : G){ C[e.from]++; C[e.to]++; } for(auto& e : G) if(std::make_pair(C[e.from],e.from) > std::make_pair(C[e.to],e.to)) e.reverse(); auto adj = G.getAdjacencyArray(false); std::fill(C.begin(), C.end(), n); for(int i=0; i void EnumerateTrianglesForEdge(Graph G, F query){ int n = G.numVertices(); std::vector C(n); for(auto e : G){ C[e.from]++; C[e.to]++; } for(auto& e : G) if(std::make_pair(C[e.from],e.from) > std::make_pair(C[e.to],e.to)) e.reverse(); auto adj = G.getEdgeIndexArray(false); std::fill(C.begin(), C.end(), -1); for(int x=0; x= 0) query(e, ee, C[z]); } } for(int e : adj[x]) C[G[e].to] = -1; } } } // namespace nachia namespace nachia { template std::vector InducedSize4SubgraphCounter2(nachia::Graph g){ int n = g.numVertices(); int m = g.numEdges(); std::vector deg(n, 0); for(auto [u,v] : g){ deg[u]++; deg[v]++; } std::vector c3_foredge(m, 0); nachia::EnumerateTrianglesForEdge(g, [&](int e1, int e2, int e3){ c3_foredge[e1]++; c3_foredge[e2]++; c3_foredge[e3]++; }); Int c3 = 0; for(int a : c3_foredge){ c3 += a; } c3 /= 3; std::vector c4_foredge = nachia::CountC4(n, g, std::vector(m, Int(1))); Int c4 = 0; for(Int a : c4_foredge){ c4 += a; } c4 /= 4; Int degC2 = 0; for(auto a : deg){ degC2 += Int(a) * (a-1) / 2; } Int degC3 = 0; for(auto a : deg){ degC3 += Int(a) * (a-1) * (a-2) / 6; } Int adjD = 0; for(auto [u,v] : g){ adjD += Int(deg[u] - 1) * (deg[v] - 1); } Int triC2 = 0; for(auto a : c3_foredge){ triC2 += Int(a) * (a-1) / 2; } Int triE = 0; for(int i=0; i ans(10); ans[0] = Int(n) * (n-1) * (n-2) * (n-3) / 24; ans[1] = Int(m) * (n-2) * (n-3) / 2; ans[2] = degC2 * (n-3); ans[3] = Int(m) * (m-1) / 2 - degC2; ans[4] = degC3; ans[5] = adjD - c3 * 3; ans[6] = c3 * (n-3); ans[7] = c4; ans[8] = triE; ans[9] = triC2; return ans; } } // namespace nachia namespace nachia{ // ax + by = gcd(a,b) // return ( x, - ) std::pair ExtGcd(long long a, long long b){ long long x = 1, y = 0; while(b){ long long u = a / b; std::swap(a-=b*u, b); std::swap(x-=y*u, y); } return std::make_pair(x, a); } } // namespace nachia namespace nachia{ template struct StaticModint{ private: using u64 = unsigned long long; unsigned int x; public: using my_type = StaticModint; template< class Elem > static Elem safe_mod(Elem x){ if(x < 0){ if(0 <= x+MOD) return x + MOD; return MOD - ((-(x+MOD)-1) % MOD + 1); } return x % MOD; } StaticModint() : x(0){} StaticModint(const my_type& a) : x(a.x){} StaticModint& operator=(const my_type&) = default; template< class Elem > StaticModint(Elem v) : x(safe_mod(v)){} unsigned int operator*() const noexcept { return x; } my_type& operator+=(const my_type& r) noexcept { auto t = x + r.x; if(t >= MOD) t -= MOD; x = t; return *this; } my_type operator+(const my_type& r) const noexcept { my_type res = *this; return res += r; } my_type& operator-=(const my_type& r) noexcept { auto t = x + MOD - r.x; if(t >= MOD) t -= MOD; x = t; return *this; } my_type operator-(const my_type& r) const noexcept { my_type res = *this; return res -= r; } my_type operator-() const noexcept { my_type res = *this; res.x = ((res.x == 0) ? 0 : (MOD - res.x)); return res; } my_type& operator*=(const my_type& r)noexcept { x = (u64)x * r.x % MOD; return *this; } my_type operator*(const my_type& r) const noexcept { my_type res = *this; return res *= r; } my_type pow(unsigned long long i) const noexcept { my_type a = *this, res = 1; while(i){ if(i & 1){ res *= a; } a *= a; i >>= 1; } return res; } my_type inv() const { return my_type(ExtGcd(x, MOD).first); } unsigned int val() const noexcept { return x; } static constexpr unsigned int mod() { return MOD; } static my_type raw(unsigned int val) noexcept { auto res = my_type(); res.x = val; return res; } my_type& operator/=(const my_type& r){ return operator*=(r.inv()); } my_type operator/(const my_type& r) const { return operator*(r.inv()); } }; } // namespace nachia using namespace std; using Modint = nachia::StaticModint<998244353>; pair solve(nachia::Graph g){ Modint tab[10][11] = { { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, { 0, 1, 2, 2, 3, 3, 3, 4, 4, 5, 6 }, { 0, 0, 1, 0, 3, 2, 3, 4, 5, 8, 12 }, { 0, 0, 0, 1, 0, 1, 0, 2, 1, 2, 3 }, { 0, 0, 0, 0, 1, 0, 0, 0, 1, 2, 4 }, { 0, 0, 0, 0, 0, 1, 0, 4, 2, 6, 12 }, { 0, 0, 0, 0, 0, 0, 1, 0, 1, 2, 4 }, { 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 3 }, { 0, 0, 0, 0, 0, 0, 0, 0, 1, 4, 12 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 6 } }; Modint coeff[11] = { 1, 0, 0, 0, 0, 0, 0, -Modint(3).inv(), 0, 0, 0 }; Modint xcoeff[11] = { 1, 0, 0, 0, 0, 0, 0, -Modint(3).inv(), 0, 0, 0 }; for(int i=0; i<10; i++){ for(int j=i+1; j<10; j++) xcoeff[j] -= xcoeff[i] * tab[i][j]; } auto res = nachia::InducedSize4SubgraphCounter2(g); Modint ans = 0; for(int t=0; t<10; t++) ans += res[t] * xcoeff[t]; return { coeff[7] , ans }; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int N, M; cin >> N >> M; auto graph = nachia::Graph::Input(cin, N, true, M, 1); auto [T, ans] = solve(graph); cout << T.val() << " " << ans.val() << "\n"; return 0; }