$$f(u)=\frac{f(fa_u)+1+\sum_{v\in son_u}(f(v)+1)}{d_u}\\ f(u)=1+\frac{f(fa_u)+\sum_{v\in son_u}f(v)}{d_u}\\ f(u)=\frac{1}{d_u}f(fa_u)+\frac{1}{d_u}\sum_{v\in son_u}f(v)+1\\$$

$$f(u)=\frac{1}{d_u}f(fa_u)+\frac{1}{d_u}\sum_{v\in son_u}(A_vf(u)+B_v)+1\\ f(u)=\frac{1}{d_u}f(fa_u)+\sum_{v\in son_u}\frac{B_v}{d_u}+f(u)\sum_{v\in son_u}\frac{A_v}{d_u}+1\\ (1-\sum_{v\in son_u}\frac{A_v}{d_u})f(u)=\frac{1}{d_u}f(fa_u)+\sum_{v\in son_u}\frac{B_v}{d_u}+1\\ f(u)=\frac{\frac{1}{d_u}f(fa_u)}{1-\sum_{v\in son_u}\frac{A_v}{d_u}}+\frac{\sum_{v\in son_u}\frac{B_v}{d_u}+1}{1-\sum_{v\in son_u}\frac{A_v}{d_u}}\\ f(u)=f(fa_u)\frac{\frac{1}{d_u}}{1-\sum_{v\in son_u}\frac{A_v}{d_u}}+\frac{\sum_{v\in son_u}\frac{B_v}{d_u}+1}{1-\sum_{v\in son_u}\frac{A_v}{d_u}}\\ f(u)=f(fa_u)\frac{1}{d_u-\sum_{v\in son_u}A_v}+\frac{d_u+\sum_{v\in son_u}B_v}{d_u-\sum_{v\in son_u}A_v}\\$$

$$\begin{cases} A_u=\frac{1}{d_u-\sum_{v\in son_u}A_v}\\ B_u=\frac{d_u+\sum_{v\in son_u}B_v}{d_u-\sum_{v\in son_u}A_v}\\ \end{cases}$$

### Code：

#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;

const int N=20;
const ll mod=998244353;

ll A[N],B[N],invd[N],res[1<<N];

struct Edge {int nxt,to;} G[N<<1];
inline void add(int u,int v) {
}

inline ll pow(ll x,ll y,ll res=1) {
x=(x%mod+mod)%mod;
for(;y;y>>=1,x=x*x%mod) if(y&1) res=res*x%mod;
return res;
}

template <typename _Tp> inline void IN(_Tp&x) {
char ch;bool flag=0;x=0;
while(ch=getchar(),!isdigit(ch)) if(ch=='-') flag=1;
while(isdigit(ch)) x=x*10+ch-'0',ch=getchar();
if(flag) x=-x;
}

void dfs(int u,int f,int S) {
if((1<<(u-1))&S) {A[u]=B[u]=0;return;}
ll cnt1=0,cnt2=0;
fa[u]=f;
if((v=G[i].to)!=f) dfs(v,u,S),cnt1+=A[v],cnt2+=B[v];
cnt1%=mod,cnt2%=mod;
ll res=pow(mod+1-invd[u]*cnt1,mod-2);
A[u]=invd[u]*res%mod;
B[u]=(1+invd[u]*cnt2%mod)*res%mod;
}

int main() {
IN(n),IN(q),IN(x);
for(int i=1,u,v;i<n;++i)
for(int i=1;i<=n;++i) invd[i]=pow(d[i],mod-2);
for(int i=0;i<1<<n;++i)
siz[i]=siz[i>>1]+(i&1),dfs(x,0,i),res[i]=B[x];
while(q--) {
int k,S=0;IN(k);
for(int i=1,x;i<=k;++i) IN(x),S|=(1<<(x-1));
ll ans=0;
for(int i=S;i;i=(i-1)&S)
ans+=(siz[i]&1?1:-1)*res[i];
ans=(ans%mod+mod)%mod;
printf("%lld\n",ans);
}
return 0;
}


QAQ