# bzoj4011 [HNOI2015]落忆枫音 拓扑排序+DP

## 题目传送门

https://lydsy.com/JudgeOnline/problem.php?id=4011

## 题解

#include<bits/stdc++.h>

#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back

template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b, 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b, 1 : 0;}

typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii;

template<typename I> inline void read(I &x) {
int f = 0, c;
while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
x = c & 15;
while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
f ? x = -x : 0;
}

const int N = 100000 + 7;
const int M = 200000 + 7;
const int P = 1e9 + 7;

int n, m, ex, ey, ans;
int q[N], idg[N], idg2[N], dp[N], vis[N];

struct Edge { int to, ne; } g[M]; int head[N], tot;
inline void addedge(int x, int y) { g[++tot].to = y, g[tot].ne = head[x], head[x] = tot; }

inline int smod(int x) { return x >= P ? x - P : x; }
inline void sadd(int &x, const int &y) { x += y; x >= P ? x -= P : x; }
inline int fpow(int x, int y) {
int ans = 1;
for (; y; y >>= 1, x = (ll)x * x % P) if (y & 1) ans = (ll)ans * x % P;
return ans;
}

int fac[N], inv[N], ifac[N];
inline void ycl(const int &n = ::n) {
fac[0] = 1; for (int i = 1; i <= n; ++i) fac[i] = (ll)fac[i - 1] * i % P;
inv[1] = 1; for (int i = 2; i <= n; ++i) inv[i] = (ll)(P - P / i) * inv[P % i] % P;
ifac[0] = 1; for (int i = 1; i <= n; ++i) ifac[i] = (ll)ifac[i - 1] * inv[i] % P;
}
inline int C(int x, int y) {
if (x < y) return 0;
return (ll)fac[x] * ifac[y] % P * ifac[x - y] % P;
}

inline void dfs(int x) {
vis[x] = 1;
for fec(i, x, y) {
++idg[y];
if (!vis[y]) dfs(y);
}
}

inline void work() {
ycl();
ans = 1, idg[1] = 1, ++idg[ey];
for (int i = 1; i <= n; ++i) ans = (ll)ans * idg[i] % P;
memcpy(idg2, idg, sizeof(int) * (n + 1));
memset(idg, 0, sizeof(int) * (n + 1));
dfs(ey);
int hd = 0, tl = 0;
dp[ey] = ans, q[++tl] = ey;
while (hd < tl) {
int x = q[++hd];
dp[x] = (ll)dp[x] * inv[idg2[x]] % P;
for fec(i, x, y) {
if (!--idg[y]) q[++tl] = y;
}
}
if (vis[ex]) sadd(ans, P - dp[ex]);
printf("%d\n", ans);
}

inline void init() {
int x, y;