func_code_string: #include <bits/stdc++.h>   using namespace std;   char _c;   bool _f;   template <class T>   inline void IN(T &x) {     x = 0, _f = 0;     while (_c = getchar(), !isdigit(_c))       if (_c ==  - ) _f = 1;     while (isdigit(_c)) x = x * 10 + _c -  0 , _c = getchar();     if (_f) x = -x;   }   template <class T>   inline void chkmax(T &x, T y) {     if (x < y) x = y;   }   template <class T>   inline void chkmin(T &x, T y) {     if (x > y) x = y;   }   const int N = 5e5 + 5;   const int mod = 1e9 + 7;   int mul(int x, int y) { return 1ll * x * y % mod; }   int n, cnt = 1, dep[N], parent[N], head[N];   int fa[N << 1], tof[N], nxt[N << 1], pre[N << 1], st[N], ed[N], deg[N];   struct Edge {     int nxt, to;   } G[N << 1];   void addedge(int u, int v) {     G[++cnt] = (Edge){head[u], v}, head[u] = cnt, ++deg[u];     G[++cnt] = (Edge){head[v], u}, head[v] = cnt, ++deg[v];   }   void dfs(int u, int lst) {     parent[u] = lst, dep[u] = dep[lst] + 1;     for (int v, i = head[u]; i; i = G[i].nxt)       if (v = G[i].to, v != lst) dfs(v, u), tof[v] = i ^ 1;   }   int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }   bool merge(int x, int y) {     x = find(x), y = find(y);     return (x == y) ? false : (fa[x] = y, true);   }   void solve(int x, int y) {     std ::stack<int> s;     std ::queue<int> q;     const int _x = x, _y = y;     while (dep[x] > dep[y]) q.push(tof[x]), --deg[x], x = parent[x];     while (dep[x] < dep[y]) s.push(tof[y]), --deg[y], y = parent[y];     while (x != y) {       q.push(tof[x]), --deg[x], x = parent[x];       s.push(tof[y]), --deg[y], y = parent[y];     }     --deg[x];     while (!s.empty()) q.push(s.top() ^ 1), s.pop();     int lst = q.front();     q.pop();     if (st[_x] || pre[lst]) {       puts( 0 );       exit(0);     };     st[_x] = lst, pre[lst] = -1;     while (!q.empty()) {       int now = q.front();       q.pop(), lst ^= 1;       if (pre[now] || nxt[lst] || !merge(lst, now)) {         puts( 0 );         exit(0);       };       pre[now] = lst, nxt[lst] = now, lst = now;     }     lst ^= 1;     if (ed[_y] || nxt[lst]) {       puts( 0 );       exit(0);     };     ed[_y] = lst, nxt[lst] = -1;     return;   }   int u, v, a[N];   int fac(int x) { return x == 0 ? 1 : mul(x, fac(x - 1)); }   int main() {     IN(n);     for (int i = 1; i <= n - 1; ++i) IN(u), IN(v), addedge(u, v);     for (int i = 1; i <= cnt; ++i) fa[i] = i;     dfs(1, 0);     for (int i = 1; i <= n; ++i) IN(a[i]);     for (int i = 1; i <= n; ++i)       if (a[i] == i) {         puts( 0 );         exit(0);       };     for (int i = 1; i <= n; ++i)       if (a[i]) solve(a[i], i);     int ans = 1;     for (int i = 1; i <= n; ++i) {       if (st[i] && ed[i])         if (find(st[i]) == find(ed[i]) && ~deg[i]) {           puts( 0 );           exit(0);         };       ans = mul(ans, fac(deg[i] == -1 ? 1 : deg[i]));     }     printf( %d n , ans);     return 0;   }   