Educational Codeforces Round 79 D.Santa's Bot

Educational Codeforces Round 79 D.Santa’s Bot

Santa Claus has received letters from n different kids throughout this year. Of course, each kid wants to get some presents from Santa: in particular, the i-th kid asked Santa to give them one of ki different items as a present. Some items could have been asked by multiple kids.

Santa is really busy, so he wants the New Year Bot to choose the presents for all children. Unfortunately, the Bot’s algorithm of choosing presents is bugged. To choose a present for some kid, the Bot does the following:

choose one kid x equiprobably among all n kids;
choose some item y equiprobably among all kx items kid x wants;
choose a kid z who will receive the present equipropably among all n kids (this choice is independent of choosing x and y); the resulting triple (x,y,z) is called the decision of the Bot.
If kid z listed item y as an item they want to receive, then the decision valid. Otherwise, the Bot’s choice is invalid.

Santa is aware of the bug, but he can’t estimate if this bug is really severe. To do so, he wants to know the probability that one decision generated according to the aforementioned algorithm is valid. Can you help him?

Input

The first line contains one integer n (1≤n≤10^6) — the number of kids who wrote their letters to Santa.

Then n lines follow, the i-th of them contains a list of items wanted by the i-th kid in the following format: ki ai,1 ai,2 … ai,ki (1≤ki,ai,j≤10^6), where ki is the number of items wanted by the i-th kid, and ai,j are the items themselves. No item is contained in the same list more than once.

Output

Print the probatility that the Bot produces a valid decision as follows:

Let this probability be represented as an irreducible fraction x/y. You have to print x/ymod998244353, where y^ (-1) is the inverse element of y modulo 998244353 (such integer that y⋅y^ (-1) has remainder 1 modulo 998244353).

Examples

input

2
2 2 1
1 1

output

124780545

input

5
2 1 2
2 3 1
3 2 4 3
2 1 4
3 4 3 2

output

798595483

针对每个固定的选择,机器人做出正确决定的概率为cnt/nnk,cnt为想要y玩具的小朋友的人数,k为机器人选的小朋友x想要的礼物数,将每一次决定正确的概率加起来就可得到答案,注意分数取模的(n ^ -1) mod p= n ^ (p-2) mod p:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=998244353;
const int maxn=1e6+5;

int n;
vector<ll>q[maxn];
map<ll,ll>m;

ll add(ll a, ll b) {
	a += b;
	if (a >= mod) a -= mod;
	return a;
}

ll mul(ll a,ll b){
    return a*b%mod;
}

ll p(ll a,ll b){
    ll ans=1;
    while(b){
        if(b&1) ans=mul(ans,a);
        a=mul(a,a);
        b>>=1;
    }
    return ans;
}

int main(){
   cin>>n;
   for(int i=0;i<n;i++){
        int k;
        scanf("%d",&k);
        for(int j=0;j<k;j++){
            ll u;
            scanf("%lld",&u);
            q[i].push_back(u);
            m[q[i][j]]++;
        }
   }
   ll ans=0;
   for(int i=0;i<n;i++){
      for(int j:q[i]){
            ans=add(ans,mul(mul(p(n,mod-2),p(q[i].size(),mod-2)),mul(m[j],p(n,mod-2))));
      }
   }
   cout<<ans;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旺 崽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值