結果

問題 No.2588 Increasing Record
ユーザー ecotteaecottea
提出日時 2023-12-18 20:35:04
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 252 ms / 3,000 ms
コード長 23,479 bytes
コンパイル時間 5,299 ms
コンパイル使用メモリ 286,820 KB
実行使用メモリ 59,416 KB
最終ジャッジ日時 2023-12-18 20:35:21
合計ジャッジ時間 14,286 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 1 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 2 ms
6,676 KB
testcase_09 AC 2 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 2 ms
6,676 KB
testcase_12 AC 61 ms
6,676 KB
testcase_13 AC 62 ms
6,676 KB
testcase_14 AC 68 ms
6,676 KB
testcase_15 AC 82 ms
7,360 KB
testcase_16 AC 132 ms
17,740 KB
testcase_17 AC 161 ms
27,516 KB
testcase_18 AC 196 ms
37,612 KB
testcase_19 AC 232 ms
40,392 KB
testcase_20 AC 234 ms
39,524 KB
testcase_21 AC 252 ms
38,860 KB
testcase_22 AC 216 ms
38,512 KB
testcase_23 AC 221 ms
38,520 KB
testcase_24 AC 220 ms
38,608 KB
testcase_25 AC 150 ms
21,348 KB
testcase_26 AC 191 ms
31,424 KB
testcase_27 AC 216 ms
37,556 KB
testcase_28 AC 213 ms
37,328 KB
testcase_29 AC 210 ms
37,592 KB
testcase_30 AC 153 ms
31,968 KB
testcase_31 AC 170 ms
47,584 KB
testcase_32 AC 183 ms
57,072 KB
testcase_33 AC 199 ms
59,196 KB
testcase_34 AC 185 ms
59,396 KB
testcase_35 AC 185 ms
59,412 KB
testcase_36 AC 179 ms
59,416 KB
testcase_37 AC 242 ms
38,380 KB
testcase_38 AC 158 ms
23,876 KB
testcase_39 AC 176 ms
35,944 KB
testcase_40 AC 147 ms
36,152 KB
testcase_41 AC 142 ms
36,580 KB
testcase_42 AC 143 ms
36,696 KB
testcase_43 AC 141 ms
37,592 KB
testcase_44 AC 126 ms
21,796 KB
testcase_45 AC 146 ms
30,512 KB
testcase_46 AC 189 ms
36,096 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifndef HIDDEN_IN_VS // 折りたたみ用

// 警告の抑制
#define _CRT_SECURE_NO_WARNINGS

// ライブラリの読み込み
#include <bits/stdc++.h>
using namespace std;

// 型名の短縮
using ll = long long; using ull = unsigned long long; // -2^63 ~ 2^63 = 9 * 10^18(int は -2^31 ~ 2^31 = 2 * 10^9)
using pii = pair<int, int>;	using pll = pair<ll, ll>;	using pil = pair<int, ll>;	using pli = pair<ll, int>;
using vi = vector<int>;		using vvi = vector<vi>;		using vvvi = vector<vvi>;	using vvvvi = vector<vvvi>;
using vl = vector<ll>;		using vvl = vector<vl>;		using vvvl = vector<vvl>;	using vvvvl = vector<vvvl>;
using vb = vector<bool>;	using vvb = vector<vb>;		using vvvb = vector<vvb>;
using vc = vector<char>;	using vvc = vector<vc>;		using vvvc = vector<vvc>;
using vd = vector<double>;	using vvd = vector<vd>;		using vvvd = vector<vvd>;
template <class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
using Graph = vvi;

// 定数の定義
const double PI = acos(-1);
const vi DX = { 1, 0, -1, 0 }; // 4 近傍(下,右,上,左)
const vi DY = { 0, 1, 0, -1 };
int INF = 1001001001; ll INFL = 4004004003104004004LL; // (int)INFL = 1010931620;

// 入出力高速化
struct fast_io { fast_io() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(18); } } fastIOtmp;

// 汎用マクロの定義
#define all(a) (a).begin(), (a).end()
#define sz(x) ((int)(x).size())
#define lbpos(a, x) (int)distance((a).begin(), std::lower_bound(all(a), x))
#define ubpos(a, x) (int)distance((a).begin(), std::upper_bound(all(a), x))
#define Yes(b) {cout << ((b) ? "Yes\n" : "No\n");}
#define YES(b) {cout << ((b) ? "YES\n" : "NO\n");}
#define rep(i, n) for(int i = 0, i##_len = int(n); i < i##_len; ++i) // 0 から n-1 まで昇順
#define repi(i, s, t) for(int i = int(s), i##_end = int(t); i <= i##_end; ++i) // s から t まで昇順
#define repir(i, s, t) for(int i = int(s), i##_end = int(t); i >= i##_end; --i) // s から t まで降順
#define repe(v, a) for(const auto& v : (a)) // a の全要素(変更不可能)
#define repea(v, a) for(auto& v : (a)) // a の全要素(変更可能)
#define repb(set, d) for(int set = 0, set##_ub = 1 << int(d); set < set##_ub; ++set) // d ビット全探索(昇順)
#define repis(i, set) for(int i = lsb(set), bset##i = set; i >= 0; bset##i -= 1 << i, i = lsb(bset##i)) // set の全要素(昇順)
#define repp(a) sort(all(a)); for(bool a##_perm = true; a##_perm; a##_perm = next_permutation(all(a))) // a の順列全て(昇順)
#define smod(n, m) ((((n) % (m)) + (m)) % (m)) // 非負mod
#define uniq(a) {sort(all(a)); (a).erase(unique(all(a)), (a).end());} // 重複除去
#define EXIT(a) {cout << (a) << endl; exit(0);} // 強制終了
#define inQ(x, y, u, l, d, r) ((u) <= (x) && (l) <= (y) && (x) < (d) && (y) < (r)) // 矩形内判定

// 汎用関数の定義
template <class T> inline ll pow(T n, int k) { ll v = 1; rep(i, k) v *= n; return v; }
template <class T> inline bool chmax(T& M, const T& x) { if (M < x) { M = x; return true; } return false; } // 最大値を更新(更新されたら true を返す)
template <class T> inline bool chmin(T& m, const T& x) { if (m > x) { m = x; return true; } return false; } // 最小値を更新(更新されたら true を返す)
template <class T> inline T get(T set, int i) { return (set >> i) & T(1); }

// 演算子オーバーロード
template <class T, class U> inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template <class T> inline istream& operator>>(istream& is, vector<T>& v) { repea(x, v) is >> x; return is; }
template <class T> inline vector<T>& operator--(vector<T>& v) { repea(x, v) --x; return v; }
template <class T> inline vector<T>& operator++(vector<T>& v) { repea(x, v) ++x; return v; }

#endif // 折りたたみ用


#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;

#ifdef _MSC_VER
#include "localACL.hpp"
#endif

//using mint = modint1000000007;
using mint = modint998244353;
//using mint = modint; // mint::set_mod(m);

namespace atcoder {
	inline istream& operator>>(istream& is, mint& x) { ll x_; is >> x_; x = x_; return is; }
	inline ostream& operator<<(ostream& os, const mint& x) { os << x.val(); return os; }
}
using vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>; using vvvvm = vector<vvvm>;
#endif


#ifdef _MSC_VER // 手元環境(Visual Studio)
#include "local.hpp"
#else // 提出用(gcc)
inline int popcount(int n) { return __builtin_popcount(n); }
inline int popcount(ll n) { return __builtin_popcountll(n); }
inline int lsb(int n) { return n != 0 ? __builtin_ctz(n) : -1; }
inline int lsb(ll n) { return n != 0 ? __builtin_ctzll(n) : -1; }
inline int msb(int n) { return n != 0 ? (31 - __builtin_clz(n)) : -1; }
inline int msb(ll n) { return n != 0 ? (63 - __builtin_clzll(n)) : -1; }
#define dump(...)
#define dumpel(v)
#define dump_list(v)
#define dump_mat(v)
#define input_from_file(f)
#define output_to_file(f)
#define Assert(b) { if (!(b)) while (1) cout << "OLE"; }
#endif


//【グラフの入力】O(n + m)
/*
* (始点, 終点) の組からなる入力を受け取り,n 頂点 m 辺のグラフを構築して返す.
*
* n : グラフの頂点の数
* m : グラフの辺の数(省略すれば n-1)
* undirected : 無向グラフか(省略すれば true)
* one_indexed : 入力が 1-indexed か(省略すれば true)
*/
Graph read_Graph(int n, int m = -1, bool undirected = true, bool one_indexed = true) {
	// verify : https://atcoder.jp/contests/tessoku-book/tasks/tessoku_book_bi

	Graph g(n);
	if (m == -1) m = n - 1;

	rep(i, m) {
		int a, b;
		cin >> a >> b;

		if (one_indexed) { --a; --b; }

		g[a].push_back(b);
		if (undirected) g[b].push_back(a);
	}

	return g;
}


//【加算 Union-Find】
/*
* Union_find_add<T>(int n) : O(n)
*	非連結で大きさ n の加算 Union-Find を値 0 で初期化する.
*
* merge(int a, int b) : O(log n)
*	頂点 a と頂点 b を統合する.
*
* bool same(int a, int b) : O(log n)
*	頂点 a と頂点 b が同じ連結成分に属するかを返す.
*
* int leader(int a) : O(log n)
*	頂点 a の属する連結成分の親を返す.
*
* int size(int a) : O(log n)
*	頂点 a の属する連結成分の大きさを返す.
*
* int size() : O(1)
*	連結成分の個数を返す.
*
* void add(int a, T val) : O(log n)
*	頂点 a を含む連結成分全体に val を加算する.
*
* T get(int a) : O(log n)
*	頂点 a の値を返す.
*
* vvi groups() : O(n log n)
*	連結成分のリストを返す.
*/
template <class T>
struct Union_find_add {
	int n; // 頂点の個数
	int m; // 連結成分の個数

	// parent_or_size[i] : 頂点 i の親または属する集合の大きさ
	//	頂点 i が根でない場合は親の番号(非負)を,
	//	根の場合は属する連結成分の大きさの -1 倍(負)を表す.
	vi parent_or_size;

	vector<T> lazy; // 遅延させている値

	// 非連結で大きさ n の Union-Find を構築する.
	Union_find_add(int n_) : n(n_), m(n), parent_or_size(n, -1), lazy(n) {}
	Union_find_add() : n(0), m(0) {} // ダミー

	// 頂点 a, b を結合する.
	void merge(int a, int b) {
		// verify : https://yukicoder.me/problems/no/1054

		// 頂点 a, b の属する連結成分の根 ra, rb を得る.
		int ra = leader(a);
		int rb = leader(b);

		// 根が同じであれば既に連結であるから何もしない.
		if (ra == rb) return;

		// 根が異なる場合,大きい連結成分の根を改めて ra,小さい方を rb とする.
		if (-parent_or_size[ra] < -parent_or_size[rb]) swap(ra, rb);

		// 小さい方の連結成分を ra を根とする連結成分に統合する.
		parent_or_size[ra] += parent_or_size[rb];
		lazy[rb] -= lazy[ra];
		parent_or_size[rb] = ra;

		// 連結成分の数を 1 つ減らす.
		m--;
	}

	// 頂点 a, b が同じ連結成分に属するかを返す.
	bool same(int a, int b) {
		// 根が同じなら連結である.
		return leader(a) == leader(b);
	}

	// 頂点 a の属する連結成分の根を返す.
	int leader(int a) {
		// a が根であれば自分自身を返す.
		int pa = parent_or_size[a];
		if (pa < 0) return a;

		// a が根でなければ,a の親 pa の根 ra を求める.
		int ra = leader(pa);

		// 経路短絡はしない.
		return ra;
	}

	void add(int a, T val) {
		// verify : https://yukicoder.me/problems/no/1054

		// a の根に遅延評価をセットする.
		int ra = leader(a);
		lazy[ra] += val;
	}

	T get(int a) {
		// verify : https://yukicoder.me/problems/no/1054

		// a から根までの遅延評価を集める.
		T res = 0;
		while (parent_or_size[a] >= 0) {
			res += lazy[a];
			a = parent_or_size[a];
		}
		res += lazy[a];
		return res;
	}

	// 頂点 a の属する連結成分の大きさを返す.
	int size(int a) {
		// a の根を調べ,そこに記録されている大きさの情報を返す.
		return -parent_or_size[leader(a)];
	}

	// 連結成分の個数を返す.
	int size() {
		return m;
	}

	// 連結成分のリストを返す.
	vvi groups() {
		vvi res(m);

		vi r_to_i(n, -1); int i = 0;
		rep(a, n) {
			int r = leader(a);
			if (r_to_i[r] == -1) r_to_i[r] = i++;
			res[r_to_i[r]].push_back(a);
		}

		return res;
	}

#ifdef _MSC_VER
	friend ostream& operator<<(ostream& os, Union_find_add d) {
		repe(g, d.groups()) {
			repe(v, g) os << v << ":" << d.get(v) << " ";
			os << endl;
		}
		return os;
	}
#endif
};


void WA() {
	int n, m;
	cin >> n >> m;

	auto g = read_Graph(n, m);

	Union_find_add<ll> d(n);
	dump(d);

	rep(s, n) {
		ll nval = 1;
		repe(t, g[s]) {
			if (t < s) {
				ll val = d.get(t);
				d.add(t, -val);
				d.merge(s, t);
				nval += 2 * val;
			}
		}

		d.add(s, nval);
		dump(d);
	}

	cout << d.get(n - 1) << endl;
}


void TLE() {
	int n, m;
	cin >> n >> m;

	vector<unordered_set<int>> g(n);

	rep(j, m) {
		int u, v;
		cin >> u >> v;
		u--; v--;

		g[u].insert(v);
		g[v].insert(u);
	}

	vm dp(n); mint res;
	dump(dp); dumpel(g);

	rep(s, n) {
		dump("----", s, "----");

		vi sml;
		repe(t, g[s]) if (t < s) sml.push_back(t);

		int S = s; mint val = dp[s] + 1; res += val;

		repe(t, sml) {
			// マージテクのつもり.でもなんか違うような・・・
			if (sz(g[S]) < sz(g[t])) {
				repe(t2, g[S]) {
					if (t2 != S) g[t2].erase(S);
					if (t2 != t) g[t2].insert(t);
					if (t != t2) g[t].insert(t2);
				}
				S = t;
			}
			else {
				repe(t2, g[t]) {
					if (t2 != t) g[t2].erase(t);
					if (t2 != S) g[t2].insert(S);
					if (S != t2) g[S].insert(t2);
				}
			}
		}

		// これが遅いのでは?
		repe(t, g[S]) {
			if (t <= s) continue;

			dp[t] += val;
		}
		dump(dp); dumpel(g);
	}

	cout << res << endl;
}


//【グラフのデカルト木】O(n + m α(n))
/*
* 与えられた無向グラフ g に対し,以下の規則で構築される n-1 を根とする有向根付き木 T を返す:
*	頂点 p に隣接する p 未満の頂点のみからなる各連結成分 S に対し,S 内の番号最大の頂点 s を p の子とする.
* 
* 性質:
*	g で s 以下の頂点のみからなるパス s-t が存在する ⇔ T で t は s の子孫
*	特に g で隣接する 2 頂点は T で先祖-子孫の関係にある.
*/
Graph graph_cartesian_tree(const Graph& g) {
	int n = sz(g);

	Graph g2(n);

	dsu d(n); 
	
	// v_max[l] : l をリーダーとする連結成分内の最大頂点番号
	vi v_max(n); iota(all(v_max), 0);

	rep(s, n) {		
		repe(t, g[s]) {
			if (t > s) continue;

			// 既に s と連結済なら何もしない.
			if (d.same(s, t)) continue;

			// s の子を t を含む連結成分内の最大頂点とする.
			g2[s].push_back(v_max[d.leader(t)]);

			// s と t を連結する.
			d.merge(s, t);
		}

		// s を含む連結成分内の最大頂点は s である.
		v_max[d.leader(s)] = s;
	}

	return g2;
}


//【オイラーツアー】
/*
* Euler_tour(Graph g, int rt) : O(n)
*	rt を根とする根付き木 g で初期化する.
*
* int lca(int s, int t) : O(log n)
*	頂点 s, t の最小共通祖先を返す.
*
* int dist(int s, int t) : O(log n)
*	頂点 s, t 間の距離を返す.
*
* int jump(int s, int t, int i) : O(log n)
*	頂点 s から t までのパスの i 番目(0-indexed)の頂点を返す(なければ -1)
*
* sort_by_DFS_order(vi& vs) : O(log |vs|)
*	頂点集合 vs を DFS 昇順にソートする.
*
* int get_in(int s) : O(1)
*	rt からの DFS で最初に頂点 s を訪れた時刻(根なら 0)を返す.
*
* int get_out(int s) : O(1)
*	rt からの DFS で最後に頂点 s から離れた時刻(根なら 2n-1)を返す.
*
* int get_pos(int t) : O(1)
*	rt からの DFS で時刻 t(∈[0..2n-1))に居た頂点の番号を返す.
*
* int get_dep(int s) : O(1)
*	頂点 s の深さを返す.
*/
pii op_ET(pii a, pii b) { return min(a, b); }
pii e_ET() { return { INF, -1 }; }
template <class G>
class Euler_tour {
	int n;

	// in[s]  : rt からの DFS で最初に頂点 s を訪れた時刻(根なら 0)
	// out[s] : rt からの DFS で最後に頂点 s から離れた時刻(根なら 2n-1)
	// pos[t] : rt からの DFS で時刻 t に居た頂点の番号(長さ 2n-1)
	// dep[s] : 頂点 s の深さ
	vi in, out, pos, dep;

	// seg[t] : 時刻 t に居た頂点の (深さ, 番号)
	using SEG = segtree<pii, op_ET, e_ET>;
	SEG seg;

	void dfs(const G& g, int rt) {
		int time = 0;

		function<void(int, int)> rf = [&](int s, int p) {
			// s を最初に訪れた
			in[s] = time;
			pos[time] = s;
			time++;

			repe(t, g[s]) {
				if (t == p) continue;

				dep[t] = dep[s] + 1;
				rf(t, s);
				pos[time] = s;
				time++;
			}

			// s から最後に離れる
			out[s] = time;
		};

		// 根から順に探索する.
		rf(rt, -1);
	}

public:
	// rt を根とする根付き木 g で初期化する.
	Euler_tour(const G& g, int rt) : n(sz(g)), in(n), out(n), pos(2 * n - 1), dep(n) {
		// verify : https://judge.yosupo.jp/problem/lca

		dfs(g, rt);

		vector<pii> ini(2 * n - 1);
		rep(t, 2 * n - 1) ini[t] = { dep[pos[t]], pos[t] };
		seg = SEG(ini);
	}
	Euler_tour() {}

	// 頂点 s, t の最小共通祖先を返す.
	int lca(int s, int t) const {
		// verify : https://judge.yosupo.jp/problem/lca

		// 初めて s または t に訪れたとき
		int l = min(in[s], in[t]);

		// 最後に s または t から離れたとき
		int r = max(out[s], out[t]);

		// その途中で訪れたことのある最も浅い頂点が最小共通祖先
		return seg.prod(l, r).second;
	}

	// 頂点 s, t 間の距離を返す.
	int dist(int s, int t) const {
		// verify : https://yukicoder.me/problems/no/2337

		int p = lca(s, t);

		// 根からの距離(深さ)の和を求め,ダブっている分を引く.
		return dep[s] + dep[t] - 2 * dep[p];
	}

	// 頂点 s から t までのパスの i 番目(0-indexed)の頂点を返す(なければ -1)
	int jump(int s, int t, int i) const {
		// verify : https://judge.yosupo.jp/problem/jump_on_tree

		int p = lca(s, t);
		int ds = dep[s], dt = dep[t], dp = dep[p];
		int dist = ds + dt - 2 * dp;

		int res;

		if (i < 0 || i > dist) res = -1;
		else if (i <= ds - dp) {
			int j = seg.max_right(out[s] - 1, [&](pii tmp) { return tmp.first > ds - i; });
			res = pos[j];
		}
		else {
			int j = seg.min_left(in[t] + 1, [&](pii tmp) { return tmp.first >= dt - (dist - i); });
			res = pos[j];
		}

		return res;
	}

	// 頂点集合 vs を DFS 昇順にソートする.
	void sort_by_DFS_order(vi& vs) {
		sort(all(vs), [&](int s, int t) { return in[s] < in[t]; });
	}

	inline int get_in(int s) const {
		return in[s];
	}

	inline int get_out(int s) const {
		return out[s];
	}

	inline int get_pos(int t) const {
		return pos[t];
	}

	inline int get_dep(int s) const {
		return dep[s];
	}
};


//【フェニック木(アーベル群)】
/*
* Fenwick_tree<S, op, o, inv>(int n) : O(n)
*	a[0..n) = o() で初期化する.要素はアーベル群 (S, op, o, inv) の元とする.
*
* Fenwick_tree<S, op, o, inv>(vS a) : O(n)
*	配列 a[0..n) で初期化する.
*
* set(int i, S x) : O(log n)
*	a[i] = x とする.
*
* S get(int i) : O(log n)
*	a[i] を返す.
*
* S sum(int l, int r) : O(log n)
*	Σa[l..r) を返す.空なら o() を返す.
*
* add(int i, S x) : O(log n)
*	a[i] += x とする.
*
* int max_right(function<bool(S)>& f) : O(log n)
*	f( Σa[0..r) ) = true となる最大の r を返す.
*   制約:f( o() ) = true,f は単調
*/
template <class S, S(*op)(S, S), S(*o)(), S(*inv)(S)>
class Fenwick_tree {
	// 参考:https://algo-logic.info/binary-indexed-tree/

	// ノードの個数(要素数 + 1)
	int n;

	// v[i] : Σa[*..i] の値(i:1-indexed,v[0] は不使用)
	vector<S> v;

	// Σa[1..r] を返す.空なら o() を返す.(r:1-indexed)
	S sum_sub(int r) const {
		S res = o();

		// 根に向かって累積 op() をとっていく.
		while (r > 0) {
			res = op(res, v[r]);

			// r の最下位ビットから 1 を減算することで次の位置を得る.
			r -= r & -r;
		}
		return res;
	}

public:
	// a[0..n) = o() で初期化する.
	Fenwick_tree(int n_) : n(n_ + 1), v(n, o()) {}

	// 配列 a[0..n) で初期化する.
	Fenwick_tree(const vector<S>& a) : n(sz(a) + 1), v(n) {
		// verify : https://judge.yosupo.jp/problem/point_add_range_sum

		// 配列の値を仮登録する.
		rep(i, n - 1) v[i + 1] = a[i];

		// 正しい値になるよう根に向かって累積 op() をとっていく.
		for (int pow2 = 1; 2 * pow2 < n; pow2 *= 2) {
			for (int i = 2 * pow2; i < n; i += 2 * pow2) {
				v[i] = op(v[i], v[i - pow2]);
			}
		}
	}
	Fenwick_tree() : n(0) {}

	// a[i] = x とする.(i : 0-indexed)
	void set(int i, S x) {
		// 差分を求める.
		S d = op(x, inv(get(i)));

		add(i, d);
	}

	// a[i] を返す.(i : 0-indexed)
	S get(int i) const {
		return sum(i, i + 1);
	}

	// Σa[l..r) を返す.空なら o() を返す.(l, r : 0-indexed)
	S sum(int l, int r) const {
		// verify : https://judge.yosupo.jp/problem/point_add_range_sum

		if (l >= r) return o();

		// 0-indexed での半開区間 [l, r) は,
		// 1-indexed での閉区間 [l + 1, r] に対応する.
		// よって閉区間 [1, r] の総和から閉区間 [1, l] の総和を引けば良い.
		return op(sum_sub(r), inv(sum_sub(l)));
	}

	// a[i] += x とする.(i : 0-indexed)
	void add(int i, S x) {
		// verify : https://judge.yosupo.jp/problem/point_add_range_sum

		// i を 1-indexed に直す.
		i++;

		// 根に向かって値を op() していく.
		while (i < n) {
			v[i] = op(v[i], x);

			// i の最下位ビットに 1 を加算することで次の位置を得る.
			i += i & -i;
		}
	}

	// f( Σa[0..r) ) = true となる最大の r を返す.(r : 0-indexed)
	int max_right(const function<bool(S)>& f) const {
		// verify : https://www.spoj.com/problems/ALLIN1/

		S x = o();

		// 注目している閉区間は [l+1, r] で幅は len
		int l = 0;
		for (int len = 1 << msb(n - 1); len > 0; len = len >> 1) {
			int r = l + len;

			if (r < n && f(op(x, v[r]))) {
				x = op(x, v[r]);
				l = r;
			}
		}
		return l;
	}

#ifdef _MSC_VER
	friend ostream& operator<<(ostream& os, const Fenwick_tree& ft) {
		rep(i, ft.n - 1) {
			os << ft.get(i) << " ";
		}
		return os;
	}
#endif
};


//【[1点,1辺]加算/[根からのパス]総和クエリ(アーベル群)】
/*
* Path_sum_query<S, op, o, inv>(Graph g, int rt) : O(n)
*	rt を根とする根付き木 g と値 o() で初期化する.
*	要素はアーベル群 (S, op, o, inv) の元とする.
*
* add_vertex(int s, S val) : O(log n)
*	頂点 s に val を加算する.
*
* add_edge(int s, S val) : O(log n)
*	頂点 s を子とする辺に val を加算する.
*
* S sum_root_path(int s) : O(log n)
*	根 rt から頂点 s まで(両端含む)の頂点と辺の値の総和を返す.
*
* S get(int s) : O(log n)
*	頂点 s の値を返す.
* 
* 利用:【フェニック木(アーベル群)】
*/
template <class S, S(*op)(S, S), S(*o)(), S(*inv)(S)>
class Path_sum_query {
	// 参考:https://perogram.hateblo.jp/entry/2020/10/01/034136

	int n;

	// in[s]  : 根からの DFS で s に最初に入った時刻
	// out[s] : 根からの DFS で s から最後に出た時刻
	// p[s]   : s の親(根なら -1)
	vi in, out, p;

	// ft[t] : 時刻 t に居た頂点の値
	Fenwick_tree<S, op, o, inv> ft;

	// ユニークオイラーツアー
	void euler_tour(const Graph& g, int rt) {
		int time = 0;

		function<void(int)> rf = [&](int s) {
			// s を最初に訪れた
			in[s] = time;
			time++;

			repe(t, g[s]) {
				if (t == p[s]) continue;
				p[t] = s;

				rf(t);
			}

			// s から最後に離れる
			out[s] = time;
		};

		// 根から順に探索する.
		p[rt] = -1;
		rf(rt);
	}

public:
	// rt を根とする根付き木 g と値 o() で初期化する.
	Path_sum_query(const Graph& g, int rt) : n(sz(g)), in(n), out(n), p(n), ft(n) {
		// verify : https://onlinejudge.u-aizu.ac.jp/courses/library/5/GRL/all/GRL_5_D

		euler_tour(g, rt);
	}

	// 頂点 s に val を加算する.
	void add_vertex(int s, S val) {
		// いもす法のように,部分木 s にいる間だけ val が累積和に寄与するようにする.
		ft.add(in[s], val);
		ft.add(out[s], -val);
	}

	// 頂点 s を子とする辺に val を加算する.
	void add_edge(int s, S val) {
		// verify : https://onlinejudge.u-aizu.ac.jp/courses/library/5/GRL/all/GRL_5_D

		// 根からのパスについては
		//		s を子とする辺を通る ⇔ s を通る
		// なので,代わりに頂点 s に val を加算する.
		add_vertex(s, val);
	}
	
	// 根 r から s までの頂点と辺の値の総和を返す.
	S sum_root_path(int s) const {
		// verify : https://onlinejudge.u-aizu.ac.jp/courses/library/5/GRL/all/GRL_5_D

		return ft.sum(0, in[s] + 1);
	}

	// 頂点 s の値を返す.
	S get(int s) const {
		S res = sum_root_path(s);
		if (p[s] != -1) res = op(res, inv(sum_root_path(p[s])));
		return res;
	}

	// 頂点 s の親を返す(なければ -1)
	int get_parent(int s) const {
		return p[s];
	}

#ifdef _MSC_VER
	friend ostream& operator<<(ostream& os, Path_sum_query Q) {
		rep(s, Q.n) os << Q.get(s) << " ";
		return os;
	}
#endif
};


//【総和 アーベル群】
/* verify : https://atcoder.jp/contests/aising2019/tasks/aising2019_d */
using S601 = mint;
S601 op601(S601 a, S601 b) { return a + b; }
S601 e601() { return 0; }
S601 inv601(S601 a) { return -a; }
#define Sum_group S601, op601, e601, inv601


int main() {
//	input_from_file("input.txt");
//	output_to_file("output.txt");

	int n, m;
	cin >> n >> m;

	auto g = read_Graph(n, m);

	auto g2 = graph_cartesian_tree(g);
	dumpel(g2);

	Euler_tour E(g2, n - 1);

	Path_sum_query<Sum_group> P(g2, n - 1);
	dump(P);

	mint inv2 = mint(2).inv();

	mint res;

	rep(s, n) {
		dump("---", s, "---");

		mint val = 1;
		
		vi ts;
		repe(t, g[s]) if (t < s) ts.push_back(t);
		
		E.sort_by_DFS_order(ts);
		dump("ts:", ts);

		rep(i, sz(ts)) {
			val += P.sum_root_path(ts[i]);

			if (i > 0) {
				int l = E.lca(ts[i], ts[i - 1]);
				val -= P.sum_root_path(l);
			}
			dump(val);
		}

		P.add_vertex(s, val);
		res += val;

		dump(P);
	}

	cout << res << endl;
}
0