1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include <algorithm>
#include <cstddef>
#include <functional>
#include <iostream>
#include <stack>
#include <utility>
#include <vector>

int main() {
	std::ios_base::sync_with_stdio(false);
	std::cin.tie(nullptr);
	int n, m, a, b;
	std::cin >> n >> m >> a >> b;
	std::vector<std::vector<int>> graph(n);
	for (int i = 0; i < m; i++) {
		int u, v;
		char d;
		std::cin >> u >> d >> d >> v;
		u--;
		v--;
		graph[u].push_back(v);
		if (d == '-')
			graph[v].push_back(u);
	}

	// Tarjan

	std::vector<std::vector<int>> sscs;
	std::vector<bool> visited(n), stacked(n);
	std::vector<int> order(n), low(n), ssc_of(n);
	std::stack<int> s;
	int ind = 0;
	std::function<void(int)> tarjan = [&](int node) {
		visited[node] = true;
		order[node] = low[node] = ind++;
		stacked[node] = true;
		s.push(node);
		for (const auto& edge : graph[node]) {
			if (!visited[edge]) {
				tarjan(edge);
				if (low[edge] < low[node])
					low[node] = low[edge];
			} else if (stacked[edge]) {
				if (order[edge] < low[node])
					low[node] = order[edge];
			}
		}
		if (low[node] == order[node]) {
			int ssc_id = (int)sscs.size();
			sscs.emplace_back();
			auto& ssc = sscs.back();
			int other;
			do {
				other = s.top();
				s.pop();
				stacked[other] = false;
				ssc.push_back(other);
				ssc_of[other] = ssc_id;
			} while (other != node);
		}
	};
	for (int i = 0; i < n; i++) {
		if (!visited[i])
			tarjan(i);
	}
	for (auto&& ssc : sscs) {
		std::sort(ssc.begin(), ssc.end());
	}

	// Condense graph

	int k = (int)sscs.size();
	std::vector<std::vector<int>> condensed_graph(k);
	for (int i = 0; i < k; i++) {
		const auto& ssc = sscs[i];
		auto& con_node = condensed_graph[i];
		for (const auto& node : ssc) {
			for (const auto& edge : graph[node]) {
				if (ssc_of[edge] != i)
					con_node.push_back(ssc_of[edge]);
			}
		}
		std::sort(con_node.begin(), con_node.end());
		con_node.erase(std::unique(con_node.begin(), con_node.end()), con_node.end());
	}

	// Topological sort

	std::vector<int> con_topol_sorted;
	std::vector<bool> con_visited(k);
	con_topol_sorted.reserve(k);
	std::function<void(int)> dfs = [&](int node) {
		con_visited[node] = true;
		for (const auto& child : condensed_graph[node]) {
			if (!con_visited[child])
				dfs(child);
		}
		con_topol_sorted.push_back(node);
	};
	for (int i = 0; i < k; i++) {
		if (!con_visited[i])
			dfs(i);
	}
	std::reverse(con_topol_sorted.begin(), con_topol_sorted.end());

	// Lake nodes in each ssc

	std::vector<std::pair<int, int>> ssc_lake_nodes(k);
	for (int i = 0; i < k; i++) {
		const auto& ssc = sscs[i];
		if (ssc.front() < a) {
			auto& [left, right] = ssc_lake_nodes[i];
			left = right = ssc.front();
			for (std::size_t j = 1; j < ssc.size(); j++) {
				const auto& node = ssc[j];
				if (node >= a)
					break;
				if (node == right + 1) {
					right = node;
				} else {
					left = node;
					break;
				}
			}
			right++;
		}
	}

	// One pass

	auto condense_ranges = [&](std::vector<std::pair<int, int>>& ranges) {
		std::pair<int, int> result;
		if (!ranges.empty()) {
			auto& [left, right] = result;
			std::sort(ranges.begin(), ranges.end());
			left = right = ranges.front().first;
			for (auto& [other_left, other_right] : ranges) {
				if (other_right < other_left)
					other_right += a;
				if (other_left <= right) {
					if (other_right > right)
						right = other_right;
				} else {
					left = other_left;
					break;
				}
			}
			if (right - left >= a)
				result = {0, a};
			else if (right > a)
				right -= a;
		}
		return result;
	};
	std::vector<std::vector<std::pair<int, int>>> all_ranges(k);
	for (const auto& node : con_topol_sorted) {
		auto& range = ssc_lake_nodes[node];
		if (range.second != 0)
			all_ranges[node].push_back(range);
		range = condense_ranges(all_ranges[node]);
		const auto& [left, right] = range;
		if (right != 0) {
			for (const auto& child : condensed_graph[node]) {
				all_ranges[child].push_back(range);
			}
		}
	}

	// Filter

	const int mod = 1'000'000'007;
	int r = 1;
	std::vector<std::pair<int, int>> filtered_ranges;
	filtered_ranges.reserve(b);
	for (int i = a; i < a + b; i++) {
		const auto& range = ssc_lake_nodes[ssc_of[i]];
		const auto& [left, right] = range;
		if (right == 0)
			r = r * 2 % mod;
		else if (left < right)
			filtered_ranges.push_back(range);
		else
			filtered_ranges.emplace_back(left, right + a);
	}
	std::sort(filtered_ranges.begin(), filtered_ranges.end());

	// DP

	auto tree_size = a * 4;
	while (tree_size & (tree_size - 1)) {
		tree_size &= tree_size - 1;
	}
	std::vector<int> tree(tree_size * 2);
	auto get_range_sum = [&](std::size_t a, std::size_t b) {
		int r = 0;
		a += tree_size;
		b += tree_size;
		while (a < b) {
			if (a & 1) {
				r += tree[a];
				r %= mod;
				a++;
			}
			if (b & 1) {
				b--;
				r += tree[b];
				r %= mod;
			}
			a >>= 1;
			b >>= 1;
		}
		return r;
	};
	int ans = 0;
	for (std::size_t i = 0; i < filtered_ranges.size(); i++) {
		const auto& [first_left, first_right] = filtered_ranges[i];
		std::fill(tree.begin(), tree.end(), 0);
		for (int i = tree_size + first_right; i; i >>= 1) {
			tree[i] = 1;
		}
		for (std::size_t j = i + 1; j < filtered_ranges.size(); j++) {
			const auto& [left, right_to_trunc] = filtered_ranges[j];
			auto right = std::min(first_left + a, right_to_trunc);
			int add = get_range_sum(left, right + 1);
			for (auto i = tree_size + right; i; i >>= 1) {
				tree[i] = (tree[i] + add) % mod;
			}
		}
		ans = (ans + tree[tree_size + first_left + a]) % mod;
	}
	std::cout << ((long long)r * ans % mod) << '\n';
	return 0;
}