# FFT が遅い mod = 998244353 g = 3 ginv = 332748118 W = [pow(g, (mod-1)>>i, mod) for i in range(24)] Winv = [pow(ginv, (mod-1)>>i, mod) for i in range(24)] def fft(k, f): for l in range(k, 0, -1): d = 1< 0: cv.append((i, cl[i])) cmax = max(cmax, i) # 想定解通りにやります O(N^1.5 log N) ? わからんぬ d = [1] * (n+1) for i in range(n+1): if cmax > i: d[i] = 0 continue for j, cnt in cv: d[i] *= pow(fact[i] * factinv[i-j] % mod, cnt, mod) d[i] %= mod # またまたFFTによって練習の日を固定したときの数え上げを計算します O(N log N) f = [d[i] * factinv[i] % mod for i in range(n + 1)] g = [(-1) ** (i % 2) * factinv[i] % mod for i in range(n + 1)] fg = convolution(f, g) ans = 0 for i in range(n + 1): ans += fact[i] * fg[i] % mod print(ans % mod)