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 $k_i$ 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 $k_x$ 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 \le n \le 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: $k_i$ $a_{i, 1}$ $a_{i, 2}$ ... $a_{i, k_i}$ ($1 \le k_i, a_{i, j} \le 10^6$), where $k_i$ is the number of items wanted by the $i$-th kid, and $a_{i, j}$ are the items themselves. No item is contained in the same list more than once.

It is guaranteed that $\sum \limits_{i = 1}^{n} k_i \le 10^6$.


-----Output-----

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

Let this probability be represented as an irreducible fraction $\frac{x}{y}$. You have to print $x \cdot y^{-1} \mod 998244353$, where $y^{-1}$ is the inverse element of $y$ modulo $998244353$ (such integer that $y \cdot 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

Difficulty: MEDIUM_HARD
Tags: Combinatorics

Mathematics

Probability

Starter code:

Solutions:
import sys
input = sys.stdin.buffer.readline
modulo = 998244353

def invMod(n):
	return pow(n, modulo - 2, modulo)

def fracMod(a, b):
	return a * invMod(b) % modulo
n = int(input())
wishList = [list(map(int, input().split())) for _ in range(n)]
wanted = [0] * (10 ** 6 + 2)
for wish in wishList:
	for present in wish[1:]:
		wanted[present] += 1
denom = pow(n, 2, modulo)
nom = 0
for wish in wishList:
	k = wish[0]
	invK = invMod(k)
	add = 0
	for present in wish[1:]:
		add += wanted[present]
	nom = (nom + add * invK) % modulo
ans = fracMod(nom, denom)
print(ans)


from sys import stdin
from collections import deque
mod = 10 ** 9 + 7
import sys
from bisect import bisect_right
from bisect import bisect_left
from collections import defaultdict
from math import sqrt, factorial, gcd, log2, inf, ceil
import heapq
input = lambda : sys.stdin.readline().rstrip()
p = 998244353
l = []
n = int(input())
for i in range(n):
	la = list(map(int, input().split()))
	l.append(la[1:])
hash = [0] * 1001001
for i in l:
	for j in i:
		hash[j] += 1
ans = 0
for i in range(n):
	ans = (ans + sum([hash[a] for a in l[i]]) * pow(len(l[i]), p - 2, p)) % p
ans = ans * pow(n * n, p - 2, p) % p
print(ans)


from collections import Counter
from fractions import Fraction
mod = 998244353

def calc_ans(x, y):
	return x * pow(y, mod - 2, mod) % mod
n = int(input())
item_list = [list(map(int, input().split()))[1:] for i in range(n)]
counter = Counter([i for items in item_list for i in items])
ans = Fraction(0)
numer = 0
denom = 1
for kid in range(n):
	l = len(item_list[kid])
	s = sum((counter[i] for i in item_list[kid]))
	numer = (numer * l + denom * s) % mod
	denom = denom * l % mod
denom = denom * n * n % mod
print(calc_ans(numer, denom))


from math import gcd
from collections import Counter
import sys
reader = (map(int, line.split()) for line in sys.stdin)
input = reader.__next__
modulo = 998244353

def lcm(lst):
	res = lst[0]
	for el in lst[1:]:
		res = res * el // gcd(res, el)
	return res

def invMod(n):
	return pow(n, modulo - 2, modulo)

def fracMod(a, b):
	return a * invMod(b) % modulo
(n,) = input()
length_lst = []
presentWanted = [0] * (10 ** 6 + 2)
wishList = []
for i in range(n):
	(k, *a) = input()
	length_lst.append(k)
	wishList.append(a)
	for present in a:
		presentWanted[present] += 1
lcm_len = lcm(length_lst)
denom = lcm_len % modulo
denom *= pow(n, 2, modulo)
denom %= modulo
nom = 0
for wish in wishList:
	wish_len = len(wish)
	f = lcm_len // wish_len
	f %= modulo
	for present in wish:
		nom += f * presentWanted[present]
		nom %= modulo
ans = fracMod(nom, denom)
print(ans)


def main():
	from array import array
	from sys import stdout, stdin
	n = int(stdin.readline())
	zero = array('i', (0,))
	ns = zero * 1000001
	ks = zero * n
	inp = [array('i')] * n
	for i in range(n):
		(k, a) = stdin.readline().split(maxsplit=1)
		a = array('i', map(int, a.split()))
		for ai in a:
			ns[ai] += 1
		ks[i] = int(k)
		inp[i] = a
	ans = 0
	for i in range(n):
		cur = 0
		for inp_i in inp[i]:
			cur = (cur + ns[inp_i]) % 998244353
		ans = (ans + pow(ks[i], 998244351, 998244353) * cur % 998244353) % 998244353
	stdout.write(f'{pow(n, 998244350, 998244353) * ans % 998244353}')
main()


from fractions import Fraction
import collections
MOD = 998244353

def egcd(a, b):
	if a == 0:
		return (b, 0, 1)
	else:
		(g, y, x) = egcd(b % a, a)
		return (g, x - b // a * y, y)

def modinv(a, m):
	(g, x, y) = egcd(a, m)
	if g != 1:
		raise Exception('modular inverse does not exist')
	else:
		return x % m
nkids = int(input())
cnts = collections.defaultdict(int)
pickprobs = collections.defaultdict(int)
for i in range(nkids):
	ls = list(map(int, input().split()))[1:]
	md = modinv(len(ls), MOD)
	for e in ls:
		cnts[e] += 1
		pickprobs[e] += md
res = 0
mdd = modinv(nkids ** 2, MOD)
for k in list(cnts.keys()):
	res += cnts[k] * pickprobs[k] * mdd
print(res % MOD)


mod = 998244353

def extgcd(a, b):
	r = [1, 0, a]
	w = [0, 1, b]
	while w[2] != 1:
		q = r[2] // w[2]
		r2 = w
		w2 = [r[0] - q * w[0], r[1] - q * w[1], r[2] - q * w[2]]
		r = r2
		w = w2
	return [w[0], w[1]]

def mod_inv(a, m=mod):
	x = extgcd(a, m)[0]
	return (m + x % m) % m
import sys
input = sys.stdin.readline
M = 10 ** 6
N = int(input())
invN = mod_inv(N)
NUM = [0] * (M + 1)
P = [0] * (M + 1)
for _ in range(N):
	(X, *A) = map(int, input().split())
	invX = mod_inv(X)
	for a in A:
		NUM[a] += 1
		P[a] = (P[a] + invN * invX % mod) % mod
ans = 0
for n in range(1, M + 1):
	ans = (ans + NUM[n] * invN % mod * P[n] % mod) % mod
print(ans)


import sys
input = sys.stdin.buffer.readline

def main():
	n = int(input())
	AA = []
	K = [0] * n
	d = {}
	for i in range(n):
		temp = list(map(int, input().split()))
		k = temp[0]
		A = temp[1:]
		for a in A:
			if a in d:
				d[a] += 1
			else:
				d[a] = 1
		AA.append(A)
		K[i] = k
	x = 0
	mod = 998244353
	for i in range(n):
		s = 0
		for a in AA[i]:
			s += d[a]
		x += s * pow(K[i] * n ** 2, mod - 2, mod) % mod
	print(x % mod)
main()


import sys
readline = sys.stdin.readline
MOD = 998244353
N = int(readline())
wants = [tuple(map(int, readline().split()))[1:] for _ in range(N)]
Q = [0] * (10 ** 6 + 1)
P = [0] * (10 ** 6 + 1)
for i in range(N):
	k = len(wants[i])
	kinv = pow(k, MOD - 2, MOD)
	for w in wants[i]:
		P[w] += 1
		Q[w] = (Q[w] + kinv) % MOD
res = 0
for i in range(10 ** 6 + 1):
	res = (res + P[i] * Q[i]) % MOD
print(pow(N ** 2, MOD - 2, MOD) * res % MOD)


import sys, os, io

def rs():
	return sys.stdin.readline().rstrip()

def ri():
	return int(sys.stdin.readline())

def ria():
	return list(map(int, sys.stdin.readline().split()))

def ws(s):
	sys.stdout.write(s + '\n')

def wi(n):
	sys.stdout.write(str(n) + '\n')

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
import math, datetime, functools, itertools, operator, bisect, fractions, statistics
from collections import deque, defaultdict, OrderedDict, Counter
from fractions import Fraction
from decimal import Decimal
from sys import stdout
from heapq import heappush, heappop, heapify, _heapify_max, _heappop_max, nsmallest, nlargest

def main():
	mod = 998244353
	starttime = datetime.datetime.now()
	if os.path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	tc = 1
	for _ in range(tc):
		kids = ri()
		z = []
		t = []
		for i in range(kids):
			a = ria()
			t += a[1:]
			z.append(a)
		d = Counter(t)
		ways = 0
		prob = 0
		for i in range(kids):
			p = kids * z[i][0] * kids
			for j in range(z[i][0]):
				prob = (prob + d[z[i][j + 1]] * pow(p, mod - 2, mod) + mod) % mod
		wi(prob % mod)
	endtime = datetime.datetime.now()
	time = (endtime - starttime).total_seconds() * 1000
	if os.path.exists('input.txt'):
		print('Time:', time, 'ms')

class FastReader(io.IOBase):
	newlines = 0

	def __init__(self, fd, chunk_size=1024 * 8):
		self._fd = fd
		self._chunk_size = chunk_size
		self.buffer = io.BytesIO()

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self, size=-1):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

class FastWriter(io.IOBase):

	def __init__(self, fd):
		self._fd = fd
		self.buffer = io.BytesIO()
		self.write = self.buffer.write

	def flush(self):
		os.write(self._fd, self.buffer.getvalue())
		(self.buffer.truncate(0), self.buffer.seek(0))

class FastStdin(io.IOBase):

	def __init__(self, fd=0):
		self.buffer = FastReader(fd)
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

class FastStdout(io.IOBase):

	def __init__(self, fd=1):
		self.buffer = FastWriter(fd)
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.flush = self.buffer.flush
sys.stdin = FastStdin()
sys.stdout = FastStdout()
main()


import sys
input = lambda : sys.stdin.readline().rstrip()
n = int(input())
mod = 998244353
child = [[int(val) for val in input().split()[1:]] for _ in range(n)]
mp = [0] * 1001001
for i in range(n):
	for kid in child[i]:
		mp[kid] += 1
ret = 0
for i in range(n):
	ret = (ret + sum((mp[val] for val in child[i])) * pow(len(child[i]), mod - 2, mod)) % mod
print(ret * pow(n * n, mod - 2, mod) % mod)


MOD = 998244353
import math
import sys

def powr(n, N):
	temp = 1
	while N > 0:
		if N % 2 != 0:
			temp = temp * n % MOD
		n = n * n % MOD
		N = N // 2
	return temp % MOD

def MODI(a, b):
	ans = powr(a, b) % MOD
	return ans
n = int(input())
pos = dict()
L = []
for _ in range(n):
	s = list(map(int, sys.stdin.readline().strip().split()))
	L.append(s)
	for i in range(1, len(s)):
		if pos.get(s[i]) == None:
			pos[s[i]] = 1
		else:
			pos[s[i]] = (pos[s[i]] + 1) % MOD
num = []
den = []
tot = 0
for i in range(0, len(L)):
	c = 0
	for j in range(1, len(L[i])):
		c = (c + pos[L[i][j]]) % MOD
	tot = (tot + c * MODI(L[i][0] % MOD % MOD, MOD - 2) % MOD) % MOD
ans = MODI(n * n % MOD, MOD - 2) % MOD
ans = ans * tot % MOD
print(ans % MOD)


import sys
input = sys.stdin.readline
MOD = 998244353
n = int(input())
info = [list(map(int, input().split())) for i in range(n)]
ans = 0
memo = {}
for i in range(n):
	for j in info[i][1:]:
		if j - 1 not in memo:
			memo[j - 1] = 1
		else:
			memo[j - 1] += 1
memo_p = {}
student_p = 1 * pow(n, MOD - 2, MOD)
pow_memo = {}
for i in range(n):
	k = info[i][0]
	if k not in pow_memo:
		pow_memo[k] = pow(k, MOD - 2, MOD)
	for j in info[i][1:]:
		if j - 1 not in memo_p:
			memo_p[j - 1] = pow_memo[k]
		else:
			memo_p[j - 1] += pow_memo[k]
		memo_p[j - 1] %= MOD
for i in memo:
	ans += memo[i] * memo_p[i]
	ans %= MOD
print(ans * student_p * student_p % MOD)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def mi(x):
	global m
	return pow(x, m - 2, m)
n = int(input())
l = []
from collections import defaultdict
d = defaultdict(int)
for i in range(n):
	ll = list(map(int, input().split()))
	for j in ll[1:]:
		d[j] += 1
	l.append(ll)
ans = 0
m = 998244353
for i in range(n):
	for j in l[i][1:]:
		ans += mi(l[i][0]) * d[j]
		ans %= m
ans *= mi(n * n)
ans %= m
print(ans)


import sys
input = sys.stdin.readline

def inv(x):
	return pow(x, M - 2, M)
M = 998244353
N = int(input())
A = [0] * 1000100
cnt = []
for i in range(N):
	temp = list(map(int, input().split()))
	cnt.append(temp[1:])
	for j in range(1, temp[0] + 1):
		A[temp[j]] += 1
ans = 0
for boy in cnt:
	total_child = len(boy)
	num = 0
	for item in boy:
		num += A[item]
	num *= inv(total_child)
	ans += num
	ans %= M
ans *= inv(N) * inv(N) % M
print(ans % M)


from math import gcd
from sys import stdin
input = stdin.readline
mod = 998244353
n = int(input())
a = []
for i in range(n):
	a.append(list(map(int, input().split())))
b = [0] * (10 ** 6 + 1)
for i in a:
	for j in i[1:]:
		b[j] += 1
x = []
for i in a:
	su = 0
	for j in i[1:]:
		su += b[j]
	r = gcd(su, i[0])
	x.append([su // r, i[0] // r])
ans = 0
n = n ** 2
a = 0
b = 1
for i in x:
	p = a * i[1]
	q = b * i[0]
	b = b * i[1]
	a = p + q
	r = gcd(a, b)
	a //= r
	b //= r
b *= n
r = gcd(a, b)
a //= r
b //= r
b = pow(b, mod - 2, mod)
print(a * b % mod)


import sys
input = lambda : sys.stdin.readline().rstrip()
P = 998244353
N = int(input())
X = [0] * 1001001
A = [[int(a) for a in input().split()[1:]] for _ in range(N)]
for i in range(N):
	for a in A[i]:
		X[a] += 1
ans = 0
for i in range(N):
	ans = (ans + sum([X[a] for a in A[i]]) * pow(len(A[i]), P - 2, P)) % P
print(ans * pow(N * N, P - 2, P) % P)


m = 998244353

def modinv(a):
	return pow(a, m - 2, m)
from fractions import *
import sys
n = int(sys.stdin.readline().strip())
array = []
freq = [0 for _ in range(int(1000000.0) + 1)]
for _ in range(n):
	a = list(map(int, sys.stdin.readline().strip().split()))
	for i in range(1, a[0] + 1):
		freq[a[i]] += 1
	array.append(a[1:])
c = 0
for u in array:
	c += Fraction(sum((freq[i] for i in u)), n * len(u))
c *= Fraction(1, n)
print(c.numerator * modinv(c.denominator) % m)


from collections import defaultdict
from sys import stdin
input = stdin.readline
n = int(input())
d = []
counter = defaultdict(int)
for i in range(n):
	d.append(list(map(int, input().split())))
for i in d:
	for j in range(1, i[0] + 1):
		counter[i[j]] += 1
ans = 0
mod = 998244353
for i in d:
	part = 0
	for j in range(1, i[0] + 1):
		part += counter[i[j]]
	part *= pow(i[0], mod - 2, mod)
	part %= mod
	ans += part
	ans %= mod
div = pow(n ** 2, mod - 2, mod)
ans *= div
ans %= mod
print(ans)


import sys
input = sys.stdin.buffer.readline
MOD = 998244353
n = int(input())
arr = [None for _ in range(n)]
ITEMMAX = 0
for i in range(n):
	arr[i] = [int(x) for x in input().split()][1:]
	ITEMMAX = max(ITEMMAX, max(arr[i]))
itemCnts = [0 for _ in range(ITEMMAX + 1)]
itemProbNum = [0 for _ in range(ITEMMAX + 1)]
itemProbDen = [1 for _ in range(ITEMMAX + 1)]
for i in range(n):
	for j in range(len(arr[i])):
		item = arr[i][j]
		itemCnts[item] += 1
		a = itemProbNum[item]
		b = itemProbDen[item]
		c = 1
		d = len(arr[i])
		pNum = (a * d % MOD + b * c % MOD) % MOD
		pDen = b * d % MOD
		itemProbNum[item] = pNum
		itemProbDen[item] = pDen
pNum = 0
pDen = 1
for i in range(1, ITEMMAX + 1):
	a = pNum
	b = pDen
	c = itemProbNum[i] * itemCnts[i] % MOD
	d = itemProbDen[i] * pow(n, 2, MOD) % MOD
	pNum = (a * d % MOD + b * c % MOD) % MOD
	pDen = b * d % MOD
numerator = pNum
denominator = pDen
denInvMOD = 1 % MOD * pow(denominator, MOD - 2, MOD) % MOD
ans = numerator * denInvMOD % MOD
print(ans)


import sys
from collections import Counter
mod = 998244353
n = int(input())
div_n = pow(n, mod - 2, mod)
wants = []
cnt = Counter()
for a in (list(map(int, l.split())) for l in sys.stdin):
	wants.append(a[1:])
	cnt.update(a[1:])
ans = 0
for i in range(n):
	prob = div_n * pow(len(wants[i]), mod - 2, mod) * div_n % mod
	for x in wants[i]:
		ans = (ans + prob * cnt[x]) % mod
print(ans)


from bisect import bisect_left as bl
from bisect import bisect_right as br
import heapq
import math
from collections import *
from functools import reduce, cmp_to_key
import sys
input = sys.stdin.readline
M = mod = 998244353

def factors(n):
	return sorted(list(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))))

def inv_mod(n):
	return pow(n, mod - 2, mod)

def li():
	return [int(i) for i in input().rstrip('\n').split(' ')]

def st():
	return input().rstrip('\n')

def val():
	return int(input().rstrip('\n'))

def li2():
	return [i for i in input().rstrip('\n').split(' ')]

def li3():
	return [int(i) for i in input().rstrip('\n')]
delem = defaultdict(list)
dset = defaultdict(int)
n = val()
for i in range(n):
	for j in li()[1:]:
		delem[j].append(i)
		dset[i] += 1
tot = 0
ans = 0
mul1 = inv_mod(n * n)
for i in delem:
	le = len(delem[i])
	for j in delem[i]:
		ans = (ans + le * mul1 * inv_mod(dset[j])) % mod
print(ans)


import sys
from collections import Counter
reader = (map(int, line.split()) for line in sys.stdin)
input = reader.__next__
modulo = 998244353

def invMod(n):
	return pow(n, modulo - 2, modulo)

def fracMod(a, b):
	return a * invMod(b) % modulo
(n,) = input()
presentWanted = Counter()
wishList = []
for i in range(n):
	(k, *a) = input()
	wishList.append(a)
	presentWanted.update(a)
denom = pow(n, 2, modulo)
nom = 0
for wish in wishList:
	wish_len = len(wish)
	f = invMod(wish_len)
	add = sum((presentWanted[present] for present in wish))
	nom += f * add
	nom %= modulo
ans = fracMod(nom, denom)
print(ans)


import sys
from sys import stdin, stdout
import bisect
import math
import heapq

def modinv(n, p):
	return pow(n, p - 2, p)

def cin():
	return map(int, sin().split())

def ain():
	return list(map(int, sin().split()))

def sin():
	return input()

def inin():
	return int(input())
import math

def Divisors(n):
	l = []
	for i in range(1, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n // i == i:
				l.append(i)
			else:
				l.append(i)
				l.append(n // i)
	return l
import math

def modInverse(b, m):
	g = math.gcd(b, m)
	if g != 1:
		return -1
	else:
		return pow(b, m - 2, m)

def modDivide(a, b, m):
	a = a % m
	inv = modInverse(b, m)
	if inv == -1:
		print('Division not defined')
	else:
		return inv * a % m

def main():
	t = inin()
	d = {}
	e = {}
	mod = 998244353
	for _ in range(t):
		a = ain()
		for i in range(1, len(a)):
			if a[i] not in d:
				d[a[i]] = 1
				e[a[i]] = modDivide(1, a[0], mod)
			else:
				d[a[i]] += 1
				e[a[i]] = e[a[i]] + modDivide(1, a[0], mod)
	ans = 0
	for i in d:
		a = modDivide(d[i], t, mod)
		b = a * e[i] % mod
		ans = (ans + b) % mod
	ans = modDivide(ans, t, mod)
	print(ans)
py2 = round(0.5)
if py2:
	from future_builtins import ascii, filter, hex, map, oct, zip
	range = xrange
import os, sys
from io import IOBase, BytesIO
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.getvalue())
			(self.truncate(0), self.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		if py2:
			self.write = self.buffer.write
			self.read = self.buffer.read
			self.readline = self.buffer.readline
		else:
			self.write = lambda s: self.buffer.write(s.encode('ascii'))
			self.read = lambda : self.buffer.read().decode('ascii')
			self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import sys

class ostream:

	def __lshift__(self, a):
		sys.stdout.write(str(a))
		return self
cout = ostream()
endl = '\n'

def readnumbers(zero=0):
	conv = ord if py2 else lambda x: x
	A = []
	numb = zero
	sign = 1
	i = 0
	s = sys.stdin.buffer.read()
	try:
		while True:
			if s[i] >= b'0'[0]:
				numb = 10 * numb + conv(s[i]) - 48
			elif s[i] == b'-'[0]:
				sign = -1
			elif s[i] != b'\r'[0]:
				A.append(sign * numb)
				numb = zero
				sign = 1
			i += 1
	except:
		pass
	if s and s[-1] >= b'0'[0]:
		A.append(sign * numb)
	return A
main()


import sys
from collections import Counter
reader = (map(int, line.split()) for line in sys.stdin)
input = reader.__next__
modulo = 998244353

def invMod(n):
	return pow(n, modulo - 2, modulo)

def fracMod(a, b):
	return a * invMod(b) % modulo
(n,) = input()
presentWanted = [0] * (10 ** 6 + 2)
wishList = []
for i in range(n):
	a = list(input())
	wishList.append(a)
	for present in a[1:]:
		presentWanted[present] += 1
denom = pow(n, 2, modulo)
nom = 0
for wish in wishList:
	k = wish[0]
	invK = invMod(k)
	add = sum((presentWanted[present] for present in wish[1:]))
	nom = (nom + add * invK) % modulo
ans = fracMod(nom, denom)
print(ans)


import sys
input = sys.stdin.readline

def fr(a, b):
	return a * pow(b, m - 2, m) % m
n = int(input())
m = 998244353
c = []
for i in range(n):
	a = list(map(int, input().split()))
	c.append(a)
d = [0] * (10 ** 6 + 1000)
for i in range(n):
	for j in range(1, c[i][0] + 1):
		d[c[i][j]] += 1
s = fr(0, 1)
for i in range(n):
	for j in range(1, c[i][0] + 1):
		s += fr(d[c[i][j]], n) * fr(1, c[i][0]) % m
		s = s % m
s = fr(1, n) * s % m
print(s)


from fractions import Fraction as Fr
import sys
import math
input = sys.stdin.readline

def modInverse(b, m):
	g = math.gcd(b, m)
	if g != 1:
		return -1
	else:
		return pow(b, m - 2, m)

def mod(a, b, m):
	a = a % m
	inv = modInverse(b, m)
	if inv == -1:
		return -999999999
	else:
		return inv * a % m
input = sys.stdin.readline
n = int(input())
d = [0 for i in range(10 ** 6 + 1)]
a = []
for i in range(n):
	p = [int(x) for x in input().split()]
	for j in range(1, p[0] + 1):
		d[p[j]] += 1
	a.append(p)
c = 0
ds = 0
for i in range(n):
	c = Fr(1, n)
	c = c * Fr(1, a[i][0])
	s = 0
	for j in range(1, a[i][0] + 1):
		s = s + Fr(d[a[i][j]], n)
	c = c * s
	ds = ds + c
ds = str(ds)
if '/' not in ds:
	print(int(ds) % 998244353)
	exit(0)
nr = int(ds[:ds.index('/')])
dr = int(ds[ds.index('/') + 1:])
print(mod(nr, dr, 998244353))


import sys
input = sys.stdin.readline
MOD = 998244353

def GCD(x, y):
	if x > y:
		small = y
	else:
		small = x
	for i in range(1, small + 1):
		if x % i == 0 and y % i == 0:
			gcd = i
	return gcd

def powmod(a, b, MOD):
	ret = 1
	while b:
		if b & 1:
			ret *= a
			ret %= MOD
			b -= 1
		a *= a
		a %= MOD
		b >>= 1
	return ret
N = int(input())
A = [0] * 1000001
K = [0] * N
prs = []
for i in range(N):
	T = list(map(int, input().split()))
	K[i] = T[0]
	for j in T[1:]:
		A[j] += 1
	prs.append(T[1:])
ans = 0
for i in range(N):
	for j in prs[i]:
		(a, b) = (A[j], K[i] * N * N % MOD)
		ret = a * powmod(b, MOD - 2, MOD) % MOD
		ans = (ans + ret) % MOD
print(ans)


import sys
from collections import Counter
from itertools import chain
input = lambda : sys.stdin.readline().rstrip()

def input_ints():
	return [int(_) for _ in input().split()]
INV = {}

def inv(x):
	if x not in INV:
		INV[x] = pow(x, M - 2, M)
	return INV[x]
M = 998244353
n = int(input())
a = [input_ints()[1:] for i in range(n)]
item_count = [0 for i in range(1000001)]
for i in chain(*a):
	item_count[i] += 1
ans = 0
for i in range(n):
	ans = (ans + sum((item_count[j] for j in a[i])) * inv(len(a[i]))) % M
invn = inv(n)
print(ans * invn * invn % M)


from fractions import Fraction
import sys
input = sys.stdin.readline

def modInverse(a, m):
	m0 = m
	y = 0
	x = 1
	if m == 1:
		return 0
	while a > 1:
		q = a // m
		t = m
		m = a % m
		a = t
		t = y
		y = x - q * y
		x = t
	if x < 0:
		x = x + m0
	return x
n = int(input())
l = []
for i in range(n):
	l.append(list(map(int, input().split()))[1:])
count = {}
for i in l:
	for j in i:
		if str(j) in count:
			count[str(j)] += 1
		else:
			count[str(j)] = 1
ans = Fraction(0, 1)
final = 0
for i in range(n):
	for j in l[i]:
		ans = Fraction(count[str(j)], n) * Fraction(1, len(l[i]))
		final += ans.numerator * modInverse(ans.denominator * n, 998244353) % 998244353
	final = final % 998244353
print(final)


from sys import stdin, stdout
from math import gcd
n = int(stdin.readline().strip())
cnt = [0 for i in range(1000100)]
mod = 998244353
s = [list(map(int, stdin.readline().strip().split())) for i in range(n)]
for i in range(n):
	for y in range(1, len(s[i])):
		cnt[s[i][y]] += 1
ans = [0, 1]
for i in range(n):
	for y in range(1, len(s[i])):
		j = s[i][y]
		f1 = [1 * cnt[j], s[i][0] * n * n]
		den = ans[1] * f1[1]
		ans = [den // ans[1] * ans[0] + den // f1[1] * f1[0], den]
		ans[0] %= mod
		ans[1] %= mod
print(ans[0] * pow(ans[1], mod - 2, mod) % mod)


MOD = 1000000007
MOD2 = 998244353
import sys
input = sys.stdin.readline
ii = lambda : int(input())
si = lambda : input()
dgl = lambda : list(map(int, input()))
f = lambda : map(int, input().split())
il = lambda : list(map(int, input().split()))
ls = lambda : list(input())
itmct = [0] * (10 ** 6 + 3)
n = ii()
l = [il() for i in range(n)]
for i in l:
	for j in i[1:]:
		itmct[j] += 1
inv = pow(n * n, MOD2 - 2, MOD2)
ans = 0
for i in l:
	x = pow(i[0], MOD2 - 2, MOD2)
	for j in i[1:]:
		ans = (ans + inv * x * itmct[j]) % MOD2
print(ans)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 18
MOD = 10 ** 9 + 7
from fractions import Fraction
Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()

def invmod(num, m):
	return pow(num, m - 2, m)
m = 998244353
n = int(ri())
lis = []
dic = {}
for i in range(n):
	t = Ri()
	lis.append(t[1:])
	for j in range(1, len(t)):
		dic[t[j]] = dic.get(t[j], 0) + 1
invn = invmod(n, m)
ans = 0
ans = 0
for i in range(n):
	for j in range(len(lis[i])):
		ans = (ans + invn * invmod(len(lis[i]), m) * dic[lis[i][j]] * invn % m) % m
print(ans)


import sys
input = sys.stdin.readline
mod = 998244353
n = int(input())
A = [list(map(int, input().split())) for i in range(n)]
PP = [0] * (10 ** 6 + 1)
for lista in A:
	for x in lista[1:]:
		PP[x] += 1
REV = pow(n * n, mod - 2, mod)
PK = [0] * (10 ** 6 + 1)
ANS = 0
for lista in A:
	k = lista[0]
	INV = pow(k, mod - 2, mod)
	for x in lista[1:]:
		ANS = (ANS + PP[x] * INV * REV) % mod
print(ANS)


import sys
input = sys.stdin.readline
mod = 998244353
from collections import Counter
n = int(input())
c = Counter()
total = []
a = []
for i in range(n):
	l = list(map(int, input().split()))
	total.append(l[0])
	a.append(l[1:])
	for j in range(1, len(l)):
		c[l[j]] += 1
ans = 0
y_inv = pow(n * n, mod - 2, mod)
for i in range(n):
	for j in a[i]:
		ans = (ans + c[j] * y_inv * pow(total[i], mod - 2, mod)) % mod
print(ans)


import sys
input = sys.stdin.readline

def inv(x):
	return pow(x, MOD - 2, MOD)
n = int(input())
A = []
cnt = [0] * (10 ** 6 + 100)
for _ in range(n):
	ka = list(map(int, input().split()))
	(k, a) = (ka[0], ka[1:])
	A.append(a)
	for i in range(k):
		cnt[a[i]] += 1
MOD = 998244353
ans = 0
for a in A:
	add = 0
	for ai in a:
		add += cnt[ai]
	add *= inv(len(a))
	ans += add % MOD
	ans %= MOD
ans *= inv(n) * inv(n) % MOD
ans %= MOD
print(ans)


from sys import stdin
from operator import itemgetter
n = int(input())
MOD = 998244353

def modInverse_0(a, b):
	(r0, r1, s0, s1) = (a, b, 1, 0)
	while r1 != 0:
		(q, rtmp) = divmod(r0, r1)
		stmp = s0 - q * s1
		(r0, s0) = (r1, s1)
		(r1, s1) = (rtmp, stmp)
	return s0 % b
inv = [modInverse_0(i, MOD) for i in range(1, 200)]

def modInverse(a):
	if a <= 100:
		return inv[a - 1]
	else:
		return modInverse_0(a, MOD)
cnt = [0 for i in range(1000001)]
k_inv = [0 for i in range(1000001)]
for i in range(n):
	ipt = [int(x) for x in stdin.readline().split()]
	k = ipt[0]
	k_inv_tmp = modInverse(k)
	for j in range(1, ipt[0] + 1):
		cnt[ipt[j]] += 1
		k_inv[ipt[j]] = (k_inv[ipt[j]] + k_inv_tmp) % MOD
tmp0 = (cnt[i] * k_inv[i] % MOD for i in range(1000001))
tmp = sum(tmp0)
n_inv = modInverse_0(n, MOD)
ans = tmp * n_inv * n_inv % MOD
print(ans)


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
mod = 998244353
n = int(input())
cnt = [0] * (10 ** 6 + 1)
kids = []
for i in range(n):
	(k, *a) = map(int, input().split())
	for ai in a:
		cnt[ai] += 1
	kids.append(a)
inv_n = pow(n, mod - 2, mod)
ans = 0
for i in range(n):
	k = len(kids[i])
	for ai in kids[i]:
		ans += inv_n * pow(k, mod - 2, mod) * inv_n * cnt[ai]
		ans %= mod
print(ans)


import math
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import math

def modInverse(b, m):
	g = math.gcd(b, m)
	if g != 1:
		return -1
	else:
		return pow(b, m - 2, m)

def modDivide(a, b, m):
	a = a % m
	inv = modInverse(b, m)
	if inv == -1:
		print('Division not defined')
	else:
		return inv * a % m
n = int(input())
k = [0] * n
s1 = []
d = dict()
tot = 1
for i in range(n):
	s = list(map(int, input().split()))
	s1.append(s)
	k[i] = s[0]
	tot = tot * k[i] % 998244353
	for j in range(1, len(s)):
		if s[j] in d:
			d[s[j]] += 1
		else:
			d.update({s[j]: 1})
ty = 0
for i in range(n):
	num = 0
	for j in range(k[i]):
		num += d[s1[i][j + 1]]
	num *= modDivide(tot, k[i], 998244353)
	ty += num
	ty %= 998244353
print(modDivide(ty, tot * n * n % 998244353, 998244353))


numofg = [0 for i in range(10 ** 6)]
n = int(input())
lofgifts = []
for i in range(n):
	l = input().split()
	lfi = l[1:len(l)]
	li = [int(i) for i in lfi]
	for j in li:
		numofg[j - 1] += 1
	lofgifts.append(li)
num = 0
den = 1
for i in range(n):
	for j in lofgifts[i]:
		no = num
		do = den
		num = (no * len(lofgifts[i]) + do * numofg[j - 1]) % 998244353
		den = do * len(lofgifts[i]) % 998244353
den = den * (n * n) % 998244353
cal = pow(den, 998244351, 998244353)
print(num * cal % 998244353)


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
import collections
import math
from functools import reduce
from operator import mul
MOD = 998244353

def modinv(x, p):
	return pow(x, p - 2, p)
N = int(input())
arr = [[int(x) for x in input().split()[1:]] for _ in range(N)]
cnt = collections.Counter()
for Y in arr:
	for y in Y:
		cnt[y] += 1
denom = reduce(lambda x, y: x * y % MOD, map(len, arr), 1)
inv = modinv(denom, MOD)
numer = sum((sum((cnt[a] for a in x)) * modinv(inv * len(x), MOD) for x in arr))
denom *= N * N
y_inv = modinv(denom, MOD)
print(numer * y_inv % MOD)


import sys

class DSantasBot:

	def solve(self):
		from collections import defaultdict
		MOD = 998244353

		def gcd(a, b):
			while b:
				(a, b) = (b, a % b)
			return a

		def lcm(a, b):
			return a * b // gcd(a, b)

		def lcm_g(l):
			ret = 1
			for i in range(len(l)):
				ret = lcm(ret, l[i])
			return ret
		n = int(input())
		kids = []
		k = []
		for _ in range(n):
			(kx, *ys) = [int(_) for _ in input().split()]
			kids.append(ys)
			k.append(kx)
		cnt = defaultdict(int)
		for kid in kids:
			for present in kid:
				cnt[present] += 1
		sm = [0] * n
		for i in range(n):
			sm[i] = sum((cnt[y] for y in kids[i]))
		lc = lcm_g(k)
		den = lc * n ** 2
		nom = 0
		for i in range(n):
			nom += lc * sm[i] // k[i]
		fraction_gcd = gcd(nom, den)
		nom //= fraction_gcd
		den //= fraction_gcd
		ans = nom * pow(den, MOD - 2, MOD)
		print(ans % MOD)
solver = DSantasBot()
input = sys.stdin.readline
solver.solve()


import sys

def input():
	return sys.stdin.readline().rstrip()
MOD = 998244353

def inv(X):
	return pow(X, MOD - 2, MOD)
MAX = int(1000000.0)

def slv():
	n = int(input())
	(num, den) = (0, 0)
	c = [0 for _ in range(MAX + 1)]
	stack = []
	ans = 0
	for child in range(n):
		tmp = list(map(int, input().split()))
		(k, req) = (tmp[0], tmp[1:])
		for v in req:
			c[v] += 1
		stack.append((k, req))
	for (k, req) in stack:
		cntsum = sum((c[v] for v in req))
		cntsum *= inv(k * n * n)
		ans += cntsum
		ans %= MOD
	print(ans)
	return

def main():
	t = 1
	for i in range(t):
		slv()
	return
main()


import sys

def ri(flag=0):
	if flag == 0:
		return [int(i) for i in sys.stdin.readline().split()]
	else:
		return int(sys.stdin.readline())
n = ri(1)
denom = [1 for i in range(n + 3)]
numArr = [0 for i in range(n + 3)]
d = dict()
arrG = [[] for i in range(n + 3)]
MOD = 998244353

def power(a, b, m):
	res = 1
	a = a % m
	while b > 0:
		if b & 1:
			res = res * a % m
		b = b >> 1
		a = a * a % m
	return res

def invPow(a, m):
	ret = power(a, m - 2, m)
	return ret

def gcd(a, b):
	if a == 0:
		return b
	else:
		return gcd(b % a, a)
for i in range(n):
	arr = ri()
	k = arr[0]
	arr = arr[1:]
	denom[i] = denom[i] * n * n
	denom[i] = denom[i] * k
	for gift in arr:
		if gift not in d:
			d[gift] = 1
		else:
			d[gift] += 1
		arrG[i].append(gift)
lcm = 1
for i in range(n):
	num = 0
	for j in arrG[i]:
		num += d[j]
	numArr[i] = num
	if i == 0:
		lcm = denom[i]
	else:
		lcm = lcm * denom[i] // gcd(lcm, denom[i])
numerator = 0
for i in range(n):
	fact = lcm // denom[i]
	numArr[i] *= fact
	numerator += numArr[i]
numerator = int(numerator)
lcm = int(lcm)
ans = numerator * invPow(lcm, MOD) % MOD
print(ans)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import math
a = int(input())
ans = []
num1 = 0
dum1 = 1
pri = 998244353
al = [0 for i in range(10 ** 6 + 1)]
n = a
total = 0
for i in range(a):
	z = list(map(int, input().split()))
	ans.append(z)
	for i in range(1, len(z)):
		al[z[i]] += 1
for i in range(len(ans)):
	x = ans[i]
	for j in range(1, len(x)):
		tr = al[x[j]]
		dm = n * (len(x) - 1) * n
		dm %= pri
		total += tr * pow(dm, pri - 2, pri) % pri
		total %= pri
print(total)


import sys
input = lambda : sys.stdin.readline().rstrip()
MOD = 998244353
n = int(input())
num = [0] * 1001001
vec = [[int(a) for a in input().split()[1:]] for _ in range(n)]
for i in range(n):
	for a in vec[i]:
		num[a] += 1
ans = 0
for i in range(n):
	ans = (ans + sum([num[a] for a in vec[i]]) * pow(len(vec[i]), MOD - 2, MOD) % MOD) % MOD
print(ans * pow(n * n, MOD - 2, MOD) % MOD)


from __future__ import division, print_function
import os, sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().strip().split(' '))

def msi():
	return map(str, input().strip().split(' '))

def li():
	return list(mi())

def dmain():
	sys.setrecursionlimit(100000000)
	threading.stack_size(40960000)
	thread = threading.Thread(target=main)
	thread.start()
abc = 'abcdefghijklmnopqrstuvwxyz'
abd = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
inf = float('inf')
vow = ['a', 'e', 'i', 'o', 'u']
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def getKey(item):
	return item[1]

def sort2(l):
	return sorted(l, key=getKey)

def d2(n, m, num):
	return [[num for x in range(m)] for y in range(n)]

def isPowerOfTwo(x):
	return x and (not x & x - 1)

def decimalToBinary(n):
	return bin(n).replace('0b', '')

def ntl(n):
	return [int(i) for i in str(n)]

def powerMod(x, y, p):
	res = 1
	x %= p
	while y > 0:
		if y & 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def read():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
from fractions import Fraction

def modInverse(b, m):
	g = gcd(b, m)
	if g != 1:
		return -1
	else:
		return pow(b, m - 2, m)

def mod(a, b, m):
	a = a % m
	inv = modInverse(b, m)
	if inv == -1:
		return -999999999
	else:
		return inv * a % m

def main():
	if True:
		n = ii()
		l = []
		s = []
		for i in range(n):
			x = li()
			s.append(x[0])
			l.append(x[1:])
		f = {}
		for i in range(n):
			for j in range(s[i]):
				if l[i][j] not in f:
					f[l[i][j]] = 1
				else:
					f[l[i][j]] += 1
		p = 0
		for i in range(n):
			pp = Fraction(1, s[i]) * Fraction(1, n)
			tt = 0
			for j in range(s[i]):
				tt += Fraction(f[l[i][j]], n)
			pp *= tt
			p += pp
		print(mod(p.numerator, p.denominator, 998244353) % 998244353)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import sys
input = sys.stdin.readline
n = int(input())
cnt = [0] * (10 ** 6 + 1)
a = []
for i in range(n):
	arr = list(map(int, input().split()))
	a.append(arr[1:])
	b = arr[1:]
	for j in range(arr[0]):
		cnt[b[j]] += 1
mod = 998244353
ans = 0
for i in range(n):
	k = len(a[i])
	res = 0
	inv = pow(n * n * k % mod, mod - 2, mod)
	for aa in a[i]:
		res += cnt[aa]
		res %= mod
	ans += res * inv % mod
	ans %= mod
print(ans)


from sys import stdin, stdout

def fastPow(a, b, mod):
	bin = a % mod
	p = 1
	while b > 0:
		if b % 2 == 1:
			p = p * bin % mod
		bin = bin * bin % mod
		b = b // 2
	return p
n = int(stdin.readline())
i = 0
items = []
has = [0] * 1000005
while i < n:
	x = stdin.readline().split(' ')
	k = int(x[0])
	j = 1
	a = []
	while j <= k:
		a.append(int(x[j]))
		has[int(x[j])] = has[int(x[j])] + 1
		j = j + 1
	items.append(a)
	i = i + 1
i = 0
smNum = 0
smDen = 0
mod = 998244353
while i < n:
	k = len(items[i])
	j = 0
	num = 0
	while j < k:
		num = (num + n - has[items[i][j]]) % mod
		j = j + 1
	if i == 0:
		smDen = k
		smNum = num
	else:
		smNum = (smNum * k + smDen * num) % mod
		smDen = smDen * k % mod
	i = i + 1
smDen = smDen * (n * n) % mod % mod
smNum = smDen - smNum
ans = smNum % mod * fastPow(smDen % mod, mod - 2, mod) % mod
stdout.write(str(ans))


import sys
import fractions
input = sys.stdin.readline
md = 998244353
n = int(input())
pres = dict()
for i in ' ' * n:
	L = map(int, input().split())
	c = 0
	for i in L:
		if c == 0:
			t = i
			c += 1
			continue
		if i in pres:
			(a, b) = pres[i][0]
			a = (a * t + b) % md
			b = b * t % md
			pres[i][0] = (a, b)
			pres[i][1] += 1
		else:
			pres[i] = [(1, t), 1]
prob = 0
for i in pres:
	(a, b) = pres[i][0]
	c = pres[i][1]
	bb = pow(b, md - 2, md)
	t = a * c
	prob += t * bb
dn = pow(n * n, md - 2, md)
print(dn * prob % md)


import sys
input = sys.stdin.readline

def modInverse(a, m):
	m0 = m
	y = 0
	x = 1
	if m == 1:
		return 0
	while a > 1:
		q = a // m
		t = m
		m = a % m
		a = t
		t = y
		y = x - q * y
		x = t
	if x < 0:
		x = x + m0
	return x
x = 3
a = 5
m = 998244353
from fractions import Fraction
n = int(input())
ks = []
for _ in range(n):
	temp = list(map(int, input().split()))
	ks.append(temp[1:])
g_vs_n = {}
for i in range(0, len(ks)):
	for j in range(0, len(ks[i])):
		if ks[i][j] not in g_vs_n:
			g_vs_n[ks[i][j]] = 0
		g_vs_n[ks[i][j]] += 1
final = Fraction(0, 1)
for i in range(0, n):
	temp = Fraction(0, 1)
	for j in range(len(ks[i])):
		temp += Fraction(g_vs_n[ks[i][j]], n)
	temp *= Fraction(1, len(ks[i]))
	final += temp
final *= Fraction(1, n)
print(modInverse(final.denominator, m) * final.numerator % m)


p = 998244353
import sys
input = sys.stdin.readline

def power(x, y, p):
	b = bin(y)[2:]
	start = x % p
	answer = 1
	for i in range(len(b)):
		if b[len(b) - 1 - i] == '1':
			answer = answer * start % p
		start = start * start % p
	return answer

def add_frac(x, y):
	(p1, q1) = x
	(p2, q2) = y
	num = (p1 * q2 + p2 * q1) % p
	den = q1 * q2 % p
	return [num, den]

def process(A):
	toys = {}
	counts = {}
	n = len(A)
	for i in range(n):
		m = len(A[i])
		for j in range(1, m):
			toy = A[i][j]
			if toy not in toys:
				toys[toy] = [0, 1]
			if toy not in counts:
				counts[toy] = 0
			counts[toy] += 1
			toys[toy] = add_frac(toys[toy], [1, m - 1])
	answer = [0, 1]
	for toy in toys:
		(a1, b1) = toys[toy]
		a2 = counts[toy]
		(num, den) = (a1 * a2 % p, b1 * n * n % p)
		answer = add_frac(answer, [num, den])
	(num, den) = answer
	den2 = power(den, p - 2, p)
	return num * den2 % p
n = int(input())
A = []
for i in range(n):
	row = [int(x) for x in str(input()).replace('\r\n', '').split()]
	A.append(row)
print(process(A))


import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def power(a, b, mod):
	res = 1
	while b:
		if b % 2:
			res = res * a % mod
		b //= 2
		a = a * a % mod
	return res % mod
MOD = 998244353
n = int(input())
groups = [0] * (10 ** 6 + 1)
inp = []
for i in range(n):
	a = list(map(int, input().split()))
	for j in range(1, 1 + a[0]):
		groups[a[j]] += 1
	inp.append(a)
ans = 0
for i in inp:
	for j in range(len(i)):
		if j:
			add = groups[i[j]] * power(n * n % MOD, MOD - 2, MOD) % MOD
			add = add * power(len(i) - 1, MOD - 2, MOD) % MOD
			ans = (ans + add) % MOD
print(ans)


MOD = 998244353

def exp(x):
	aux = 998244351
	a = list()
	ans = 1
	while aux:
		a += [aux % 2]
		aux //= 2
	for elem in a:
		if elem == 1:
			ans *= x
		ans %= MOD
		x *= x
		x %= MOD
	return ans
n = int(input())
present = [0 for i in range(1000001)]
child = list(list())
for i in range(n):
	line = input()
	line = line.split()
	child += [list()]
	for j in range(1, len(line)):
		pres = int(line[j])
		present[pres] += 1
		child[i] += [pres]
f1 = 0
f2 = 0
for i in range(n):
	sum = 0
	k = len(child[i])
	for pres in child[i]:
		sum += present[pres]
	if f2 == 0:
		f2 = k * n
		f1 = sum
	else:
		f1 = f1 * k * n + f2 * sum
		f2 *= k * n
		f1 %= MOD
		f2 %= MOD
f2 *= n
f2 %= MOD

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)
g = gcd(f1, f2)
f1 //= g
f2 //= g
f2 = exp(f2)
ans = f1 * f2
ans %= MOD
print(ans)


import os
import sys
from io import BytesIO, IOBase

def main():
	n = int(input())
	d = [0 for i in range(10 ** 6 + 2)]
	den = 0
	ls = []
	for i in range(n):
		l = [int(j) for j in input().split()][1:]
		ls.append(l)
		for j in l:
			d[j] += 1
	ans = 0
	P = 998244353
	for i in range(n):
		ans = (ans + sum([d[a] for a in ls[i]]) * pow(len(ls[i]), P - 2, P)) % P
	print(ans * pow(n * n, P - 2, P) % P)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


from sys import stdin

def egcd(a, b):
	if a == 0:
		return (b, 0, 1)
	else:
		(g, y, x) = egcd(b % a, a)
		return (g, x - b // a * y, y)

def modinv(a, m):
	(g, x, y) = egcd(a, m)
	if g != 1:
		raise Exception('modular inverse does not exist')
	else:
		return x % m
n = int(input())
p = 998244353
gift_count = {}
kid_req = {}
for i in range(n):
	data = list(map(int, stdin.readline().rstrip().split()))
	kid_req[i] = []
	kid_req[i] += data[1:]
	for gift in data[1:]:
		if gift not in gift_count:
			gift_count[gift] = 1
		else:
			gift_count[gift] += 1
ans = 0
for i in range(n):
	gift_req_size = len(kid_req[i])
	for gift in kid_req[i]:
		ans = ans + gift_count[gift] * modinv(gift_req_size, p) % p
ans = ans * modinv(n * n, p) % p
print(ans)


import collections
import random
import heapq
import bisect
import math
import time

class Solution2:

	def solve(self, A1, A2):
		pass

def gcd(a, b):
	if not b:
		return a
	return gcd(b, a % b)

def lcm(a, b):
	return b * a // gcd(b, a)

class Solution:

	def solve(self, kids):
		MOD = 998244353

		def inv(a):
			g = MOD
			r = a
			x = 0
			y = 1
			while r != 0:
				q = int(g / r)
				g %= r
				(g, r) = (r, g)
				x -= q * y
				(x, y) = (y, x)
			return x + MOD if x < 0 else x
		INV = inv(len(kids))
		count = collections.defaultdict(int)
		for kid in kids:
			for present in kid:
				count[present] += 1
		out = 0
		for kid in kids:
			tt = 0
			for p in kid:
				tt += count[p] * INV
			tt *= inv(len(kid))
			out += int(tt)
			out %= MOD
		return out * INV % MOD
sol = Solution()
sol2 = Solution2()
for test_case in range(1):
	s = input()
	a = []
	for _ in range(int(s)):
		a.append([int(c) for c in input().split()[1:]])
	out = sol.solve(a)
	print(str(out))


from math import gcd
import sys
input = sys.stdin.buffer.readline
modulo = 998244353

def lcm(lst):
	res = next(lst)
	while True:
		try:
			el = next(lst)
			res = res * el // gcd(res, el)
		except StopIteration:
			return res

def invMod(n):
	return pow(n, modulo - 2, modulo)

def fracMod(a, b):
	return a * invMod(b) % modulo
n = int(input())
wishList = [list(map(int, input().split())) for _ in range(n)]
wanted = [0] * (10 ** 6 + 2)
for wish in wishList:
	for present in wish[1:]:
		wanted[present] += 1
lcm_len = lcm((wish[0] for wish in wishList))
denom = lcm_len % modulo
denom *= pow(n, 2, modulo)
denom %= modulo
nom = 0
for wish in wishList:
	k = wish[0]
	f = lcm_len // k % modulo
	for present in wish[1:]:
		nom = (nom + f * wanted[present]) % modulo
ans = fracMod(nom, denom)
print(ans)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from collections import defaultdict
n = int(input())
mod = 998244353
inv = pow(n ** 2, mod - 2, mod)
prob = [0 for s in range(10 ** 6)]
tracker = [0 for s in range(10 ** 6)]
for s in range(n):
	want = list(map(int, input().split()))
	stud = defaultdict(int)
	tot = want[0]
	setty = set([])
	for i in want[1:]:
		stud[i - 1] += 1
		if not i - 1 in setty:
			tracker[i - 1] += 1
			setty.add(i - 1)
	for b in stud.keys():
		prob[b] += stud[b] * pow(tot, mod - 2, mod)
	prob[b] = prob[b] % mod
ans = 0
for s in range(10 ** 6):
	if tracker[s] >= 1:
		ans += prob[s] * tracker[s] * inv
		ans = ans % mod
print(ans)


import sys
from math import gcd

def lcm(a, b):
	return a * b // gcd(a, b)

def rint():
	return map(int, sys.stdin.readline().split())

def input():
	return sys.stdin.readline().rstrip('\n')

def oint():
	return int(input())
n = oint()
modv = 998244353
k = []
l = 1
ka = []
cnt = dict()
for i in range(n):
	ka.append(list(rint()))
	l = lcm(l, ka[i][0])
	for ki in range(1, ka[i][0] + 1):
		kk = ka[i][ki]
		if kk in cnt:
			cnt[kk] += 1
		else:
			cnt[kk] = 1
l *= n
ans1 = 0
for i in range(n):
	mul = l // (ka[i][0] * n)
	ans2 = 0
	for ki in range(1, ka[i][0] + 1):
		kk = ka[i][ki]
		ans2 += n - cnt[kk]
	ans1 += mul * ans2
	ans1 %= modv
print(pow(l * n, 998244353 - 2, 998244353) * (l * n - ans1) % 998244353)


def main():
	import sys
	input = sys.stdin.buffer.readline
	mod = 998244353
	N = int(input())
	A = []
	for _ in range(N):
		A.append(list(map(int, input().split())))
	prob = [0] * (10 ** 6 + 3)
	want = [0] * (10 ** 6 + 3)
	invN = pow(N, mod - 2, mod)
	for i in range(N):
		k = A[i][0]
		invK = pow(k, mod - 2, mod)
		for j in range(1, k + 1):
			prob[A[i][j]] = (prob[A[i][j]] + invN * invK % mod) % mod
			want[A[i][j]] += 1
	ans = 0
	for i in range(1, 10 ** 6 + 1):
		ans = (ans + prob[i] * want[i] % mod * invN % mod) % mod
	print(ans)
main()


import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop, heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 998244353
inf = float('inf')

def I():
	return int(sys.stdin.readline())

def LI():
	return list(map(int, sys.stdin.readline().split()))
n = I()
m = 0
lst = []
tmp = []
for _ in range(n):
	a = LI()
	lst.append(a)
	k = a[0]
	a = a[1:]
	m = max(m, max(a))
cnt = [0] * m
cnt2 = [0] * m
for i in range(n):
	k = lst[i][0]
	tmp2 = lst[i][1:]
	for j in tmp2:
		cnt[j - 1] += pow(k, mod - 2, mod)
		cnt[j - 1] %= mod
		cnt2[j - 1] += 1
ans = 0
f = defaultdict(lambda : 0)
for i in range(n):
	for j in lst[i][1:]:
		if f[j - 1]:
			continue
		f[j - 1] = 1
		ans += cnt2[j - 1] * pow(n, mod - 2, mod) * cnt[j - 1] * pow(n, mod - 2, mod)
		ans %= mod
print(ans)


import sys
mod = 998244353

def inv(k):
	return pow(k, mod - 2, mod)
d = {}
k = {}
n = int(input())
ans = 0
l = [0] * n
for _ in range(n):
	d[_] = list(map(int, sys.stdin.readline().split()))
	for i in d[_][1:]:
		k[i] = k.get(i, 0) + 1
for i in range(n):
	for j in d[i][1:]:
		ans += inv(d[i][0]) * (n - k[j]) % mod
		ans %= mod
ans *= inv(n)
ans %= mod
ans *= inv(n)
ans %= mod
if ans == 0:
	print(1)
	exit()
print((mod - ans + 1) % mod)


import sys
from collections import defaultdict
import math
mod = 998244353

def power(a, b):
	if b == 0:
		return 1
	p = power(a, b // 2)
	p = p * p % mod
	if b & 1:
		p = p * a % mod
	return p % mod

def modinverse(a, mod):
	return power(a, mod - 2)
dic = defaultdict(int)
n = int(sys.stdin.readline())
child = defaultdict(list)
length = defaultdict(int)
for _ in range(n):
	l = list(map(int, sys.stdin.readline().split()))
	for i in range(1, len(l)):
		dic[l[i]] += 1
		child[_ + 1].append(l[i])
	length[_ + 1] = l[0]
x = modinverse(n, mod)
ans = 0
for i in range(1, n + 1):
	nextd = modinverse(length[i], mod) % mod
	nextn = 0
	for j in range(length[i]):
		nextn += dic[child[i][j]] % mod
		nextn = nextn % mod
	ans = ans + nextn * nextd % mod
	ans = ans % mod
ans = ans * modinverse(n * n, mod) % mod
print(ans % mod)


from sys import stdin
from math import gcd

def input():
	return stdin.readline()[:-1]

def intput():
	return int(input())

def sinput():
	return input().split()

def intsput():
	return map(int, sinput())
debugging = False

def dprint(*args):
	if debugging:
		print(*args)
	else:
		pass
mod = 998244353

def inverse(x):
	return pow(x, mod - 2, mod)
n = intput()
counts = {}
aa = []
for _ in range(n):
	inputthing = list(intsput())
	k = inputthing[0]
	a = inputthing[1:]
	for i in range(k):
		if a[i] not in counts:
			counts[a[i]] = 0
		counts[a[i]] += 1
	aa.append([k, a])
ans = 0
thisisit = [0, 1]
for x in aa:
	tmp = [0, 1]
	(k, a) = x
	for v in a:
		ans += counts[v] * inverse(k)
		ans %= mod
ans *= inverse(n ** 2)
ans %= mod
print(ans)


import sys
import math
from collections import defaultdict, Counter
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def power(x, y, m):
	if y == 0:
		return 1
	p = power(x, y // 2, m) % m
	p = p * p % m
	if y % 2 == 0:
		return p
	else:
		return x * p % m
mod = 998244353
n = int(input())
d = defaultdict(int)
tot = 0
l = []
for i in range(n):
	(k, *a) = list(map(int, input().split()))
	l.append(a)
	for j in a:
		d[j] += 1
	tot += k
ans = 0
for j in l:
	p = 0
	q = power(n * len(j) * n % mod, mod - 2, mod)
	for k in j:
		p += d[k]
	ans = (ans + p * q % mod) % mod
print(ans)


import sys
mod = 998244353
chk = [0] * 1000001
numk = [0] * 1000001
n = int(input())
for j in range(n):
	d = [int(i) for i in sys.stdin.readline().split()]
	ddd = d[0]
	f = pow(ddd, mod - 2, mod)
	for i in range(1, ddd + 1):
		dd = d[i]
		numk[dd] += 1
		chk[dd] = (chk[dd] + f) % mod
ansn = 0
for i in range(1, 1000001):
	if numk[i]:
		ansn = (ansn + numk[i] * chk[i]) % mod
ansn = ansn * pow(n * n % mod, mod - 2, mod) % mod
print(ansn)


import sys
input = sys.stdin.readline
M = 998244353
N = int(input())
A = [list(map(int, input().split())) for _ in range(N)]
from fractions import Fraction

def extgcd(a, b):
	r = [1, 0, a]
	w = [0, 1, b]
	while w[2] != 1:
		q = r[2] // w[2]
		r2 = w
		w2 = [r[0] - q * w[0], r[1] - q * w[1], r[2] - q * w[2]]
		r = r2
		w = w2
	return [w[0], w[1]]

def mod_inv(a, m):
	x = extgcd(a, m)[0]
	return (m + x % m) % m

def adbwm(a, b, m):
	return a % m * mod_inv(b, m) % m
lis = [0] * 10 ** 6
for i in range(N):
	for a in A[i][1:]:
		lis[a - 1] += 1
prob = [0] * 10 ** 6
for i in range(10 ** 6):
	if lis[i] == 0:
		continue
	prob[i] = Fraction(lis[i], N)
ans = Fraction(0)
for i in range(N):
	tmp = 0
	for a in A[i][1:]:
		tmp += prob[a - 1] / A[i][0]
	ans += tmp / N
print(adbwm(ans.numerator, ans.denominator, M))


import sys
input = sys.stdin.buffer.readline
MOD = 998244353
n = int(input())
arr = [None for _ in range(n)]
ITEMMAX = 0
for i in range(n):
	arr[i] = [int(x) for x in input().split()][1:]
	ITEMMAX = max(ITEMMAX, max(arr[i]))
itemCnts = [0 for _ in range(ITEMMAX + 1)]
itemProbs = [0 for _ in range(ITEMMAX + 1)]
for i in range(n):
	for j in range(len(arr[i])):
		item = arr[i][j]
		itemCnts[item] += 1
		addedProb = 1 % MOD * pow(n * len(arr[i]), MOD - 2, MOD) % MOD
		itemProbs[item] += addedProb
		itemProbs[item] %= MOD
ans = 0
for i in range(1, ITEMMAX + 1):
	temp = itemProbs[i] * itemCnts[i] % MOD
	addedProb = temp % MOD * pow(n, MOD - 2, MOD) % MOD
	ans += addedProb
	ans %= MOD
print(ans)


import sys
p = 998244353
n = int(sys.stdin.readline().strip())
A = [0] * (10 ** 6 + 1)
a = []
k = []
for i in range(0, n):
	line = list(map(int, sys.stdin.readline().strip().split()))
	k.append(line[0])
	a.append(line[1:])
	for j in range(0, k[i]):
		x = a[i][j]
		A[x] = A[x] + 1
(r, s) = (0, 1)
for i in range(0, n):
	x = 0
	for j in range(0, k[i]):
		x = x + A[a[i][j]]
	y = k[i] * n
	(r, s) = ((r * y + s * x) % p, s * y % p)
ans = r
s = s * n
q = p - 2
while q > 0:
	if q % 2 == 1:
		ans = ans * s % p
		q = q - 1
	else:
		q = q // 2
		s = s * s % p
print(ans % p)