結果

問題 No.2892 Lime and Karin
コンテスト
ユーザー kemuniku
提出日時 2026-09-01 23:19:37
言語 Nim
(2.2.10 + ACL)
コンパイル:
nim --nimcache=~ --hints:off -o:a.out -d:release cpp _filename_
実行:
./a.out
結果
AC  
実行時間 1,197 ms / 8,000 ms
+ 858µs
コード長 63,760 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 6,371 ms
コンパイル使用メモリ 185,700 KB
実行使用メモリ 39,596 KB
最終ジャッジ日時 2026-09-01 23:20:17
合計ジャッジ時間 30,155 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 52
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import macros;macro ImportExpand(s:untyped):untyped = parseStmt($s[2])
# source: src/cplib/tmpl/citrus.nim
ImportExpand "cplib/tmpl/citrus" <=== "when not declared CPLIB_TMPL_CITRUS:\n    const CPLIB_TMPL_CITRUS* = 1\n    {.warning[UnusedImport]: off.}\n    {.hint[XDeclaredButNotUsed]: off.}\n    import os\n    import algorithm\n    import sequtils\n    import tables\n    import macros\n    import std/math\n    import sets\n    import strutils\n    import strformat\n    import sugar\n    import streams\n    import deques\n    import bitops\n    import heapqueue\n    import options\n    import hashes\n    const MODINT998244353* = 998244353\n    const MODINT1000000007* = 1000000007\n    when not declared CPLIB_UTILS_CONSTANTS:\n        const CPLIB_UTILS_CONSTANTS* = 1\n        const INF32*: int32 = 1001000027.int32\n        const INF64*: int = int(3300300300300300491)\n    \n    const INFL = INF64\n    type double* = float64\n    let readNext = iterator(getsChar: bool = false): string {.closure.} =\n        while true:\n            var si: string\n            try: si = stdin.readLine\n            except EOFError: yield \"\"\n            for s in si.split:\n                if getsChar:\n                    for i in 0..<s.len():\n                        yield s[i..i]\n                else:\n                    if s.isEmptyOrWhitespace: continue\n                    yield s\n    proc input*(t: typedesc[string]): string = readNext()\n    proc input*(t: typedesc[char]): char = readNext(true)[0]\n    proc input*(t: typedesc[int]): int = readNext().parseInt\n    proc input*(t: typedesc[float]): float = readNext().parseFloat\n    macro input*(t: typedesc, n: varargs[int]): untyped =\n        var repStr = \"\"\n        for arg in n:\n            repStr &= &\"({arg.repr}).newSeqWith \"\n        parseExpr(&\"{repStr}input({t})\")\n    macro input*(ts: varargs[auto]): untyped =\n        var tupStr = \"\"\n        for t in ts:\n            tupStr &= &\"input({t.repr}),\"\n        parseExpr(&\"({tupStr})\")\n    macro input*(n: int, ts: varargs[auto]): untyped =\n        for typ in ts:\n            if typ.typeKind != ntyAnything:\n                error(\"Expected typedesc, got \" & typ.repr, typ)\n        parseExpr(&\"({n.repr}).newSeqWith input({ts.repr})\")\n    proc `fmtprint`*(x: int or string or char or bool): string = return $x\n    proc `fmtprint`*(x: float or float32 or float64): string = return &\"{x:.16f}\"\n    proc `fmtprint`*[T](x: seq[T] or Deque[T] or HashSet[T] or set[T]): string = return x.toSeq.join(\" \")\n    proc `fmtprint`*[T, N](x: array[T, N]): string = return x.toSeq.join(\" \")\n    proc `fmtprint`*[T](x: HeapQueue[T]): string =\n        var q = x\n        while q.len != 0:\n            result &= &\"{q.pop()}\"\n            if q.len != 0: result &= \" \"\n    proc `fmtprint`*[T](x: CountTable[T]): string =\n        result = x.pairs.toSeq.mapIt(&\"{it[0]}: {it[1]}\").join(\" \")\n    proc `fmtprint`*[K, V](x: Table[K, V]): string =\n        result = x.pairs.toSeq.mapIt(&\"{it[0]}: {it[1]}\").join(\" \")\n    proc print*(prop: tuple[f: File, sepc: string, endc: string, flush: bool], args: varargs[string, `fmtprint`]) =\n        for i in 0..<len(args):\n            prop.f.write(&\"{args[i]}\")\n            if i != len(args) - 1: prop.f.write(prop.sepc) else: prop.f.write(prop.endc)\n        if prop.flush: prop.f.flushFile()\n    proc print*(args: varargs[string, `fmtprint`]) = print((f: stdout, sepc: \" \", endc: \"\\n\", flush: false), args)\n    const LOCAL_DEBUG{.booldefine.} = false\n    macro getSymbolName(x: typed): string = x.toStrLit\n    macro debug*(args: varargs[untyped]): untyped =\n        when LOCAL_DEBUG:\n            result = newNimNode(nnkStmtList, args)\n            template prop(e: string = \"\"): untyped = (f: stderr, sepc: \"\", endc: e, flush: true)\n            for i, arg in args:\n                if arg.kind == nnkStrLit:\n                    result.add(quote do: print(prop(), \"\\\"\", `arg`, \"\\\"\"))\n                else:\n                    result.add(quote do: print(prop(\": \"), getSymbolName(`arg`)))\n                    result.add(quote do: print(prop(), `arg`))\n                if i != args.len - 1: result.add(quote do: print(prop(), \", \"))\n                else: result.add(quote do: print(prop(), \"\\n\"))\n        else:\n            return (quote do: discard)\n    proc `%`*(x: SomeInteger, y: SomeInteger): int =\n        result = x mod y\n        if y > 0 and result < 0: result += y\n        if y < 0 and result > 0: result += y\n    proc `//`*(x: SomeInteger, y: SomeInteger): int =\n        result = x div y\n        if y > 0 and result * y > x: result -= 1\n        if y < 0 and result * y < x: result -= 1\n    proc `^`*(x: SomeInteger, y: SomeInteger): int = x xor y\n    proc `&`*(x: SomeInteger, y: SomeInteger): int = x and y\n    proc `|`*(x: SomeInteger, y: SomeInteger): int = x or y\n    proc `>>`*(x: SomeInteger, y: SomeInteger): int = x shr y\n    proc `<<`*(x: SomeInteger, y: SomeInteger): int = x shl y\n    proc `%=`*(x: var SomeInteger, y: SomeInteger): void = x = x % y\n    proc `//=`*(x: var SomeInteger, y: SomeInteger): void = x = x // y\n    proc `^=`*(x: var SomeInteger, y: SomeInteger): void = x = x ^ y\n    proc `&=`*(x: var SomeInteger, y: SomeInteger): void = x = x & y\n    proc `|=`*(x: var SomeInteger, y: SomeInteger): void = x = x | y\n    proc `>>=`*(x: var SomeInteger, y: SomeInteger): void = x = x >> y\n    proc `<<=`*(x: var SomeInteger, y: SomeInteger): void = x = x << y\n    proc `[]`*(x, n: int): bool = (x and (1 shl n)) != 0\n    proc `[]=`*(x: var int, n: int, i: bool) =\n        if i: x = x or (1 << n)\n        else: (if x[n]: x = x xor (1 << n))\n    proc pow*(a, n: int, m = INF64): int =\n        var\n            rev = 1\n            a = a\n            n = n\n        while n > 0:\n            if n % 2 != 0: rev = (rev * a) mod m\n            if n > 1: a = (a * a) mod m\n            n >>= 1\n        return rev\n    when not declared CPLIB_MATH_ISQRT:\n        const CPLIB_MATH_ISQRT* = 1\n        proc isqrt*(n: int): int =\n            var x = n\n            var y = (x + 1) shr 1\n            while y < x:\n                x = y\n                y = (x + n div x) shr 1\n            return x\n    \n    proc chmax*[T](x: var T, y: T): bool {.discardable.} = (if x < y: (x = y; return true; ) return false)\n    proc chmin*[T](x: var T, y: T): bool {.discardable.} = (if x > y: (x = y; return true; ) return false)\n    proc `max=`*[T](x: var T, y: T) = x = max(x, y)\n    proc `min=`*[T](x: var T, y: T) = x = min(x, y)\n    proc at*(x: char, a = '0'): int = int(x) - int(a)\n    proc Yes*(b: bool = true): void = print(if b: \"Yes\" else: \"No\")\n    proc No*(b: bool = true): void = Yes(not b)\n    proc YES_upper*(b: bool = true): void = print(if b: \"YES\" else: \"NO\")\n    proc NO_upper*(b: bool = true): void = Yes_upper(not b)\n    const DXY* = [(0, -1), (0, 1), (-1, 0), (1, 0)]\n    const DDXY* = [(1, -1), (1, 0), (1, 1), (0, -1), (0, 1), (-1, -1), (-1, 0), (-1, 1)]\n    macro exit*(statement: untyped): untyped = (quote do: (`statement`; quit()))\n    proc initHashSet[T](): Hashset[T] = initHashSet[T](0)\n"
# source: src/cplib/graph/graph.nim
ImportExpand "cplib/graph/graph" <=== "when not declared CPLIB_GRAPH_GRAPH:\n    const CPLIB_GRAPH_GRAPH* = 1\n\n    import sequtils\n    import math\n    type DynamicGraph*[T] = ref object of RootObj\n        edges*: seq[seq[(int32, T)]]\n        len*: int\n    type StaticGraph*[T] = ref object of RootObj\n        src*, dst*: seq[int32]\n        cost*: seq[T]\n        elist*: seq[(int32, T)]\n        start*: seq[int32]\n        len*: int\n\n    type WeightedDirectedGraph*[T] = ref object of DynamicGraph[T]\n    type WeightedUnDirectedGraph*[T] = ref object of DynamicGraph[T]\n    type UnWeightedDirectedGraph* = ref object of DynamicGraph[int]\n    type UnWeightedUnDirectedGraph* = ref object of DynamicGraph[int]\n    type WeightedDirectedStaticGraph*[T] = ref object of StaticGraph[T]\n    type WeightedUnDirectedStaticGraph*[T] = ref object of StaticGraph[T]\n    type UnWeightedDirectedStaticGraph* = ref object of StaticGraph[int]\n    type UnWeightedUnDirectedStaticGraph* = ref object of StaticGraph[int]\n\n    type GraphTypes*[T] = DynamicGraph[T] or StaticGraph[T]\n    type DirectedGraph* = WeightedDirectedGraph or UnWeightedDirectedGraph or WeightedDirectedStaticGraph or UnWeightedDirectedStaticGraph\n    type UnDirectedGraph* = WeightedUnDirectedGraph or UnWeightedUnDirectedGraph or WeightedUnDirectedStaticGraph or UnWeightedUnDirectedStaticGraph\n    type WeightedGraph*[T] = WeightedDirectedGraph[T] or WeightedUnDirectedGraph[T] or WeightedDirectedStaticGraph[T] or WeightedUnDirectedStaticGraph[T]\n    type UnWeightedGraph* = UnWeightedDirectedGraph or UnWeightedUnDirectedGraph or UnWeightedDirectedStaticGraph or UnWeightedUnDirectedStaticGraph\n    type DynamicGraphTypes* = WeightedDirectedGraph or UnWeightedDirectedGraph or WeightedUnDirectedGraph or UnWeightedUnDirectedGraph\n    type StaticGraphTypes* = WeightedDirectedStaticGraph or UnWeightedDirectedStaticGraph or WeightedUnDirectedStaticGraph or UnWeightedUnDirectedStaticGraph\n\n    proc add_edge_dynamic_impl*[T](g: DynamicGraph[T], u, v: int, cost: T, directed: bool) =\n        g.edges[u].add((v.int32, cost))\n        if not directed: g.edges[v].add((u.int32, cost))\n\n    proc initWeightedDirectedGraph*(N: int, edgetype: typedesc = int): WeightedDirectedGraph[edgetype] =\n        result = WeightedDirectedGraph[edgetype](edges: newSeq[seq[(int32, edgetype)]](N), len: N)\n    proc add_edge*[T](g: var WeightedDirectedGraph[T], u, v: int, cost: T) =\n        g.add_edge_dynamic_impl(u, v, cost, true)\n\n    proc initWeightedUnDirectedGraph*(N: int, edgetype: typedesc = int): WeightedUnDirectedGraph[edgetype] =\n        result = WeightedUnDirectedGraph[edgetype](edges: newSeq[seq[(int32, edgetype)]](N), len: N)\n    proc add_edge*[T](g: var WeightedUnDirectedGraph[T], u, v: int, cost: T) =\n        g.add_edge_dynamic_impl(u, v, cost, false)\n\n    proc initUnWeightedDirectedGraph*(N: int): UnWeightedDirectedGraph =\n        result = UnWeightedDirectedGraph(edges: newSeq[seq[(int32, int)]](N), len: N)\n    proc add_edge*(g: var UnWeightedDirectedGraph, u, v: int) =\n        g.add_edge_dynamic_impl(u, v, 1, true)\n\n    proc initUnWeightedUnDirectedGraph*(N: int): UnWeightedUnDirectedGraph =\n        result = UnWeightedUnDirectedGraph(edges: newSeq[seq[(int32, int)]](N), len: N)\n    proc add_edge*(g: var UnWeightedUnDirectedGraph, u, v: int) =\n        g.add_edge_dynamic_impl(u, v, 1, false)\n\n    proc len*[T](G: WeightedGraph[T]): int = G.len\n    proc len*(G: UnWeightedGraph): int = G.len\n\n    iterator `[]`*[T](g: WeightedDirectedGraph[T] or WeightedUnDirectedGraph[T], x: int): (int, T) =\n        for e in g.edges[x]: yield (e[0].int, e[1])\n    iterator `[]`*(g: UnWeightedDirectedGraph or UnWeightedUnDirectedGraph, x: int): int =\n        for e in g.edges[x]: yield e[0].int\n\n    proc add_edge_static_impl*[T](g: StaticGraph[T], u, v: int, cost: T, directed: bool) =\n        g.src.add(u.int32)\n        g.dst.add(v.int32)\n        g.cost.add(cost)\n        if not directed:\n            g.src.add(v.int32)\n            g.dst.add(u.int32)\n            g.cost.add(cost)\n\n    proc build_impl*[T](g: StaticGraph[T]) =\n        g.start = newSeqWith(g.len + 1, 0.int32)\n        for i in 0..<g.src.len:\n            g.start[g.src[i]] += 1\n        g.start.cumsum\n        g.elist = newSeq[(int32, T)](g.start[^1])\n        for i in countdown(g.src.len - 1, 0):\n            var u = g.src[i]\n            var v = g.dst[i]\n            g.start[u] -= 1\n            g.elist[g.start[u]] = (v, g.cost[i])\n    proc build*(g: StaticGraphTypes) = g.build_impl()\n\n    proc initWeightedDirectedStaticGraph*(N: int, edgetype: typedesc = int, capacity: int = 0): WeightedDirectedStaticGraph[edgetype] =\n        result = WeightedDirectedStaticGraph[edgetype](\n            src: newSeqOfCap[int32](capacity),\n            dst: newSeqOfCap[int32](capacity),\n            cost: newSeqOfCap[edgetype](capacity),\n            elist: newSeq[(int32, edgetype)](0),\n            start: newSeq[int32](0),\n            len: N\n        )\n    proc add_edge*[T](g: var WeightedDirectedStaticGraph[T], u, v: int, cost: T) =\n        g.add_edge_static_impl(u, v, cost, true)\n\n    proc initWeightedUnDirectedStaticGraph*(N: int, edgetype: typedesc = int, capacity: int = 0): WeightedUnDirectedStaticGraph[edgetype] =\n        result = WeightedUnDirectedStaticGraph[edgetype](\n            src: newSeqOfCap[int32](capacity*2),\n            dst: newSeqOfCap[int32](capacity*2),\n            cost: newSeqOfCap[edgetype](capacity*2),\n            elist: newSeq[(int32, edgetype)](0),\n            start: newSeq[int32](0),\n            len: N\n        )\n    proc add_edge*[T](g: var WeightedUnDirectedStaticGraph[T], u, v: int, cost: T) =\n        g.add_edge_static_impl(u, v, cost, false)\n\n    proc initUnWeightedDirectedStaticGraph*(N: int, capacity: int = 0): UnWeightedDirectedStaticGraph =\n        result = UnWeightedDirectedStaticGraph(\n            src: newSeqOfCap[int32](capacity),\n            dst: newSeqOfCap[int32](capacity),\n            cost: newSeqOfCap[int](capacity),\n            elist: newSeq[(int32, int)](0),\n            start: newSeq[int32](0),\n            len: N\n        )\n    proc add_edge*(g: var UnWeightedDirectedStaticGraph, u, v: int) =\n        g.add_edge_static_impl(u, v, 1, true)\n\n    proc initUnWeightedUnDirectedStaticGraph*(N: int, capacity: int = 0): UnWeightedUnDirectedStaticGraph =\n        result = UnWeightedUnDirectedStaticGraph(\n            src: newSeqOfCap[int32](capacity*2),\n            dst: newSeqOfCap[int32](capacity*2),\n            cost: newSeqOfCap[int](capacity*2),\n            elist: newSeq[(int32, int)](0),\n            start: newSeq[int32](0),\n            len: N\n        )\n    proc add_edge*(g: var UnWeightedUnDirectedStaticGraph, u, v: int) =\n        g.add_edge_static_impl(u, v, 1, false)\n\n    proc static_graph_initialized_check*[T](g: StaticGraph[T]) = assert g.start.len > 0, \"Static Graph must be initialized before use.\"\n\n    iterator `[]`*[T](g: WeightedDirectedStaticGraph[T] or WeightedUnDirectedStaticGraph[T], x: int): (int, T) =\n        g.static_graph_initialized_check()\n        for i in g.start[x]..<g.start[x+1]: yield (g.elist[i][0].int, g.elist[i][1])\n    iterator `[]`*(g: UnWeightedDirectedStaticGraph or UnWeightedUnDirectedStaticGraph, x: int): int =\n        g.static_graph_initialized_check()\n        for i in g.start[x]..<g.start[x+1]: yield g.elist[i][0].int\n\n    iterator to_and_cost*[T](g: DynamicGraph[T], x: int): (int, T) =\n        for e in g.edges[x]: yield (e[0].int, e[1])\n    iterator to_and_cost*[T](g: StaticGraph[T], x: int): (int, T) =\n        g.static_graph_initialized_check()\n        for i in g.start[x]..<g.start[x+1]: yield (g.elist[i][0].int, g.elist[i][1])\n    \n    import tables\n\n    type UnWeightedUnDirectedTableGraph*[T] = object \n        toi* : Table[T,int]\n        v* : seq[T]\n        graph* : UnWeightedUnDirectedGraph\n\n    type UnWeightedDirectedTableGraph*[T] = object \n        toi* : Table[T,int]\n        v* : seq[T]\n        graph* : UnWeightedDirectedGraph\n\n    type WeightedUnDirectedTableGraph*[T,S] = object \n        toi* : Table[T,int]\n        v* : seq[T]\n        graph* : WeightedUnDirectedGraph[S]\n\n    type WeightedDirectedTableGraph*[T,S] = object \n        toi* : Table[T,int]\n        v* : seq[T]\n        graph* : WeightedDirectedGraph[S]\n\n    type UnWeightedTableGraph*[T] = UnWeightedUnDirectedTableGraph[T] or UnWeightedDirectedTableGraph[T]\n    type WeightedTableGraph*[T,S] = WeightedUnDirectedTableGraph[T,S] or WeightedDirectedTableGraph[T,S]\n\n    proc initUnWeightedUnDirectedTableGraph*[T](V:openArray[T]):UnWeightedUnDirectedTableGraph[T]=\n        for i in 0..<len(V):\n            result.toi[V[i]] = i\n        result.graph = initUnWeightedUnDirectedGraph(len(V))\n        result.v = @V\n\n    proc initUnWeightedDirectedTableGraph*[T](V:openArray[T]):UnWeightedDirectedTableGraph[T]=\n        for i in 0..<len(V):\n            result.toi[V[i]] = i\n        result.graph = initUnWeightedDirectedGraph(len(V))\n        result.v = @V\n\n    proc initWeightedUnDirectedTableGraph*[T](V:openArray[T],S:typedesc = int):WeightedUnDirectedTableGraph[T,S]=\n        for i in 0..<len(V):\n            result.toi[V[i]] = i\n        result.graph = initWeightedUnDirectedGraph(len(V),S)\n        result.v = @V\n\n    proc initWeightedDirectedTableGraph*[T](V:openArray[T],S:typedesc = int):WeightedDirectedTableGraph[T,S]=\n        for i in 0..<len(V):\n            result.toi[V[i]] = i\n        result.graph = initWeightedDirectedGraph(len(V),S)\n        result.v = @V\n\n    proc add_edge*[T](g: var UnWeightedTableGraph[T],u,v:T)=\n        g.graph.add_edge(g.toi[u],g.toi[v])\n\n    proc add_edge*[T,S](g: var WeightedTableGraph[T,S],u,v:T,cost:S)=\n        g.graph.add_edge(g.toi[u],g.toi[v],cost)\n\n    iterator `[]`*[T,S](g: WeightedDirectedTableGraph[T,S] or WeightedUnDirectedTableGraph[T,S], x: T): (T, S) = \n        for (x,y) in g.graph[g.toi[x]]:\n            yield (g.v[x],y)\n    iterator `[]`*[T](g: UnWeightedDirectedTableGraph[T] or UnWeightedUnDirectedTableGraph[T], x: T): T = \n        for x in g.graph[g.toi[x]]:\n            yield g.v[x]\n"
# source: src/cplib/convolution/convolution.nim
ImportExpand "cplib/convolution/convolution" <=== "when not declared CPLIB_CONVOLUTION_CONVOLUTION:\n    const CPLIB_CONVOLUTION_CONVOLUTION* = 1\n    import bitops\n    import sequtils\n    import std/math\n    when not declared CPLIB_MODINT_MODINT:\n        const CPLIB_MODINT_MODINT* = 1\n        when not declared CPLIB_MODINT_MODINT_BARRETT:\n            const CPLIB_MODINT_MODINT_BARRETT* = 1\n            import std/macros\n            import std/tables\n            type StaticBarrettModint*[M: static[uint32]] = object\n                a: uint32\n            type DynamicBarrettModint*[M: static[uint32]] = object\n                a: uint32\n            type BarrettModint* = StaticBarrettModint or DynamicBarrettModint\n        \n            proc get_im*(M: uint32): uint = cast[uint](-1) div M + 1\n            var barrettParamCache {.compileTime.}: Table[uint32, NimNode]\n            var barrettCachedParam: tuple[M: uint32, im: uint]\n        \n            macro get_param*[M: static[uint32]](self: typedesc[StaticBarrettModint[M]]): untyped =\n                if M notin barrettParamCache:\n                    let value = (M.uint32, get_im(M))\n                    barrettParamCache[M] = newLit(value)\n                return barrettParamCache[M]\n            template get_param*(self: typedesc[DynamicBarrettModint]): tuple[M: uint32, im: uint] =\n                {.cast(noSideEffect).}:\n                    barrettCachedParam\n            template get_M*(T: typedesc[BarrettModint]): uint =\n                when T is StaticBarrettModint: T.M.uint\n                else: get_param(T).M.uint\n            proc setMod*[T: static[uint32]](self: typedesc[DynamicBarrettModint[T]], M: SomeInteger or SomeUnsignedInt) =\n                barrettCachedParam = (M: M.uint32, im: get_im(M.uint32))\n        \n            template umod*[T: BarrettModint](self: typedesc[T] or T): uint32 =\n                when self is typedesc:\n                    when self is StaticBarrettModint: self.M\n                    else: (get_param(self)).M\n                else: T.umod\n            template `mod`*[T: BarrettModint](self: typedesc[T] or T): int32 = (T.umod).int32\n            {.emit: \"\"\"\n            #include <cstdio>\n            inline unsigned long long calc_mul(const unsigned long long &a, const unsigned long long &b) {\n                return (unsigned long long)(((__uint128_t)(a) * b) >> 64);\n            }\n            \"\"\".}\n            proc calc_mul*(a, b: culonglong): culonglong {.importcpp: \"calc_mul(#, #)\", nodecl, inline.}\n            proc rem*(T: typedesc[BarrettModint], a: uint): uint32 =\n                when T is StaticBarrettModint:\n                    const im = get_im(T.M)\n                    const M = get_M(T)\n                    var x = (calc_mul(cast[culonglong](a), cast[culonglong](im))).uint\n                    var r = a - x * M\n                    if M <= r: r += M\n                    return cast[uint32](r)\n                else:\n                    var p = get_param(T)\n                    var x = (calc_mul(cast[culonglong](a), cast[culonglong](p.im))).uint\n                    var r = a - x * p.M\n                    if p.M <= r: r += p.M\n                    return cast[uint32](r)\n            proc init*(T: typedesc[BarrettModint], a: T or SomeInteger): auto =\n                when a is T: return a\n                else:\n                    if a in 0..<T.mod.int: return T(a: a.uint32)\n                    var a = a mod T.mod.int\n                    if a < 0: a += T.mod.int\n                    return T(a: a.uint32)\n        \n            proc `-`*[T: BarrettModint](a: T): T = T(a: T.umod - a.a)\n            proc `+=`*[T: BarrettModint](a: var T, b: T or SomeInteger) =\n                a.a += init(T, b).a\n                if a.a >= T.umod: a.a -= T.umod\n            proc `-=`*[T: BarrettModint](a: var T, b: T or SomeInteger) =\n                a.a -= init(T, b).a\n                if a.a >= T.umod: a.a += T.umod\n            proc `*=`*[T: BarrettModint] (a: var T, b: T or SomeInteger) =\n                a.a = rem(T, (a.a).uint * (init(T, b).a).uint)\n            proc inv*[T: BarrettModint](x: T): T =\n                assert x.val != 0\n                var x: int32 = int32(x.val)\n                var y: int32 = T.mod\n                var u = 1i32\n                var v, t = 0i32\n                while y > 0:\n                    t = x div y\n                    x -= t * y\n                    u -= t * v\n                    swap(x, y)\n                    swap(u, v)\n                return init(T, u)\n            proc `/=`*[T: BarrettModint](a: var T, b: T or SomeInteger) = a *= init(T, b).inv\n            proc val*(a: BarrettModint): int = a.a.int\n            macro declarStaticBarrettModint*(name, M) =\n                let converter_name = ident(\"to\" & $`name`)\n                quote do:\n                    type `name`* = StaticBarrettModint[`M`]\n                    converter `converter_name`*(a: int): StaticBarrettModint[`M`] = init(StaticBarrettModint[`M`], a)\n            macro declarDynamicBarrettModint*(name, id) =\n                let converter_name = ident(\"to\" & $`name`)\n                quote do:\n                    type `name`* = DynamicBarrettModint[`id`]\n                    converter `converter_name`*(a: int): DynamicBarrettModint[`id`] = init(DynamicBarrettModint[`id`], a)\n        \n        when not declared CPLIB_MODINT_MODINT_MONTGOMERY:\n            const CPLIB_MODINT_MODINT_MONTGOMERY* = 1\n            import std/macros\n            import std/tables\n            type StaticMontgomeryModint*[M: static[uint32]] = object\n                a: uint32\n            type DynamicMontgomeryModint*[M: static[uint32]] = object\n                a: uint32\n            type MontgomeryModint* = StaticMontgomeryModint or DynamicMontgomeryModint\n        \n            proc get_r*(M: uint32): uint32 =\n                result = M\n                for _ in 0..<4: result *= 2u32 - M * result\n            proc get_n2*(M: uint32): uint32 = uint32((not uint(M - 1u32)) mod uint(M))\n            proc check_params(M, r: uint32) =\n                assert M < (1u32 shl 30), \"invalid mod >= 2^30\"\n                assert (M and 1u32) == 1u32, \"invalid mod % 2 == 0\"\n                assert r * M == 1, \"r * mod != 1\"\n            var montgomeryParamCache {.compileTime.}: Table[uint32, NimNode]\n            var montgomeryCachedParam: tuple[M, r, n2: uint32]\n            macro get_param*[M: static[uint32]](self: typedesc[StaticMontgomeryModint[M]]): untyped =\n                if M notin montgomeryParamCache:\n                    let value = (M.uint32, get_r(M), get_n2(M))\n                    montgomeryParamCache[M] = newLit(value)\n                return montgomeryParamCache[M]\n            template get_param*(self: typedesc[DynamicMontgomeryModint]): tuple[M, r, n2: uint32] =\n                # FIXME: cast(noSideEffect)を付けないと、set_of_mint.join(\" \")とかで死ぬ。\n                # もうちょっと筋の良い解決方法があればそうしたい\n                {.cast(noSideEffect).}:\n                    montgomeryCachedParam\n            template get_M*(T: typedesc[MontgomeryModint]): uint32 =\n                when T is StaticMontgomeryModint: T.M\n                else: get_param(T).M\n            proc setMod*[T: static[uint32]](self: typedesc[DynamicMontgomeryModint[T]], M: SomeInteger or SomeUnsignedInt) =\n                var r = get_r(M.uint32)\n                var n2 = get_n2(M.uint32)\n                montgomeryCachedParam = (M: M.uint32, r: get_r(M.uint32), n2: n2)\n                check_params(M.uint32, r)\n            template umod*[T: MontgomeryModint](self: typedesc[T] or T): uint32 =\n                when self is typedesc:\n                    when self is StaticMontgomeryModint: self.M\n                    else: get_param(self).M\n                else: T.umod\n            template `mod`*[T: MontgomeryModint](self: typedesc[T] or T): int32 = (T.umod).int32\n        \n            proc reduce(T: typedesc[StaticMontgomeryModint], b: uint): uint32 =\n                let (_, r, _) = get_param(T)\n                return cast[uint32]((b + uint(cast[uint32](b) * (not (r - 1u32))) * T.M) shr 32)\n            proc reduce(T: typedesc[DynamicMontgomeryModint], b: uint): uint32 =\n                var p = get_param(T)\n                return cast[uint32]((b + uint(cast[uint32](b) * (not (p.r - 1u32))) * p.M) shr 32)\n            proc init*(T: typedesc[MontgomeryModint], a: T or SomeInteger): auto =\n                when a is T: return a\n                elif T is StaticMontgomeryModint:\n                    let (_, r, n2) = get_param(T)\n                    check_params(T.M, r)\n                    var ai = reduce(T, uint(a.int32 mod T.M.int32 + T.M.int32) * n2)\n                    result = StaticMontgomeryModint[T.M](a: ai)\n                elif T is DynamicMontgomeryModint:\n                    var p = get_param(T)\n                    var ai = reduce(T, uint(a.int32 mod p.M.int32 + p.M.int32) * p.n2)\n                    result = DynamicMontgomeryModint[T.M](a: ai)\n        \n            proc `+=`*[T: MontgomeryModint](a: var T, b: T or SomeInteger) =\n                a.a += init(T, b).a - T.get_M * 2u32\n                if cast[int32](a.a) < 0i32: a.a += T.get_M * 2u32\n            proc `-=`*[T: MontgomeryModint](a: var T, b: T or SomeInteger) =\n                a.a -= init(T, b).a\n                if cast[int32](a.a) < 0i32: a.a += T.get_M * 2u32\n            proc val*[T: MontgomeryModint](a: T): int =\n                result = reduce(T, a.a).int\n                if result.uint32 >= T.get_M: result -= T.get_M.int\n        \n            proc `-`*[T: MontgomeryModint](a: T): T = (result = init(T, 0); result -= a)\n            proc `*=`*[T: MontgomeryModint] (a: var T, b: T or SomeInteger) = a.a = reduce(T, uint(a.a) * init(T, b).a)\n            proc inv*[T: MontgomeryModint](x: T): T =\n                assert x.val != 0\n                var x: int32 = int32(x.val)\n                var y: int32 = T.mod\n                var u = 1i32\n                var v, t = 0i32\n                while y > 0:\n                    t = x div y\n                    x -= t * y\n                    u -= t * v\n                    swap(x, y)\n                    swap(u, v)\n                return init(T, u)\n            proc `/=`*[T: MontgomeryModint](a: var T, b: T or SomeInteger) = a *= init(T, b).inv\n        \n            macro declarStaticMontgomeryModint*(name, M) =\n                let converter_name = ident(\"to\" & $`name`)\n                quote do:\n                    type `name`* = StaticMontgomeryModint[`M`]\n                    converter `converter_name`*(a: int): StaticMontgomeryModint[`M`] = init(StaticMontgomeryModint[`M`], a)\n            macro declarDynamicMontgomeryModint*(name, id) =\n                let converter_name = ident(\"to\" & $`name`)\n                quote do:\n                    type `name`* = DynamicMontgomeryModint[`id`]\n                    converter `converter_name`*(a: int): DynamicMontgomeryModint[`id`] = init(DynamicMontgomeryModint[`id`], a)\n        \n        import std/math\n        import std/algorithm\n        declarStaticMontgomeryModint(modint998244353_montgomery, 998244353u32)\n        declarStaticMontgomeryModint(modint1000000007_montgomery, 1000000007u32)\n        declarDynamicMontgomeryModint(modint_montgomery, 1u32)\n        declarStaticBarrettModint(modint998244353_barrett, 998244353u32)\n        declarStaticBarrettModint(modint1000000007_barrett, 1000000007u32)\n        declarDynamicBarrettModint(modint_barrett, 1u32)\n        proc `+`*(a, b: MontgomeryModint or BarrettModint): auto = (result = a; result += b)\n        proc `-`*(a, b: MontgomeryModint or BarrettModint): auto = (result = a; result -= b)\n        proc `*`*(a, b: MontgomeryModint or BarrettModint): auto = (result = a; result *= b)\n        proc `/`*(a, b: MontgomeryModint or BarrettModint): auto = (result = a; result /= b)\n        proc `+`*(a: MontgomeryModint or BarrettModint, b: SomeInteger): auto = (result = a; result += b)\n        proc `-`*(a: MontgomeryModint or BarrettModint, b: SomeInteger): auto = (result = a; result -= b)\n        proc `*`*(a: MontgomeryModint or BarrettModint, b: SomeInteger): auto = (result = a; result *= b)\n        proc `/`*(a: MontgomeryModint or BarrettModint, b: SomeInteger): auto = (result = a; result /= b)\n        proc `+`*[ModInt: MontgomeryModint or BarrettModint](a: SomeInteger, b: Modint): auto = init(Modint, a) + b\n        proc `-`*[ModInt: MontgomeryModint or BarrettModint](a: SomeInteger, b: Modint): auto = init(Modint, a) - b\n        proc `*`*[ModInt: MontgomeryModint or BarrettModint](a: SomeInteger, b: Modint): auto = init(Modint, a) * b\n        proc `/`*[ModInt: MontgomeryModint or BarrettModint](a: SomeInteger, b: Modint): auto = init(Modint, a) / b\n        proc `/`*[ModInt: MontgomeryModint or BarrettModint](a: ModInt, b: static int): auto =\n            when ModInt is StaticMontgomeryModint or ModInt is StaticBarrettModint:\n                const tmp = init(Modint, b).inv\n                return a * tmp\n            else:\n                return a * init(Modint, b).inv\n        proc pow*(a: MontgomeryModint or BarrettModint, n: int): auto =\n            result = init(typeof(a), 1)\n            var a = a\n            var n = n\n            while n > 0:\n                if (n and 1) == 1: result *= a\n                a *= a\n                n = (n shr 1)\n        proc `$`*(a: MontgomeryModint or BarrettModint): string = $(a.val)\n        proc estimate_rational*(a: MontgomeryModint or BarrettModint, ub: int = isqrt(typeof(a).mod)): string =\n            var v: seq[tuple[s, n, d: int]]\n            for d in 1..ub:\n                var n = (a * d).val\n                if n * 2 > a.mod:\n                    n = - (a.mod - n)\n                if gcd(n, d) > 1: continue\n                v.add((n.abs + d, n, d))\n            v.sort\n            return $v[0].n & \"/\" & $v[0].d\n    \n    when not declared CPLIB_MATH_INVGCD:\n        const CPLIB_MATH_INVGCD* = 1\n        # @param b `1 <= b`\n        # @return pair(g, x) s.t. g = gcd(a, b), xa = g (mod b), 0 <= x < b/g\n        import std/math\n        proc inv_gcd*(a, b: int): (int, int) =\n            var a = floorMod(a, b)\n            if a == 0: return (b, 0)\n            var\n                s = b\n                t = a\n                m0 = 0\n                m1 = 1\n    \n            while t != 0:\n                var u = s div t\n                s -= t * u\n                m0 -= m1 * u\n    \n                var tmp = s\n                s = t\n                t = tmp\n                tmp = m0\n                m0 = m1\n                m1 = tmp\n            if m0 < 0: m0 += b div s\n            return (s, m0)\n    \n    \n\n    {.emit: \"\"\"\n#ifndef CPLIB_CONVOLUTION_AVX2_NTT_HPP\n#define CPLIB_CONVOLUTION_AVX2_NTT_HPP\n#include <immintrin.h>\n#include <algorithm>\n#include <cstddef>\n#include <cstdint>\n#include <cstring>\nusing cZ=std::size_t;\n#pragma GCC target(\"avx2,bmi2\")\n#pragma GCC optimize(\"O3\")\nnamespace cplib_avx2_ntt {\nusing u32 = std::uint32_t;\nusing u64 = std::uint64_t;\nusing V=__m256i;\nu32 modulus = 998244353U;\nu32 primitive_root = 3U;\nstruct Montgomery {\nu32 negative_inverse;\nu32 radix;\nu32 radix_squared;\nMontgomery() {\nnegative_inverse = 1;\nfor (int i = 0; i < 5; ++i) {\nnegative_inverse *= 2U + negative_inverse * modulus;\n}\nradix = (u32)((u64(1) << 32) % modulus);\nradix_squared = (u32)(u64(radix) * radix % modulus);\n}\ninline u32 multiply(u32 a, u32 b) const {\nconst u64 product = u64(a) * b;\nconst u32 correction = (u32)(product) * negative_inverse;\nu32 value = (u32)(\n(product + u64(correction) * modulus) >> 32);\nif (value >= modulus) value -= modulus;\nreturn value;\n}\ninline u32 to_montgomery(u32 value) const {\nreturn multiply(value, radix_squared);\n}\n};\ninline u32 add_mod(u32 a, u32 b) {\nconst u32 sum = a + b;\nreturn sum >= modulus ? sum - modulus : sum;\n}\ninline u32 subtract_mod(u32 a, u32 b) {\nreturn a >= b ? a - b : a + modulus - b;\n}\ninline u32 power_mod(u32 base, u32 exponent) {\nu32 result = 1;\nwhile (exponent != 0) {\nif (exponent & 1U) result = (u32)(u64(result) * base % modulus);\nbase = (u32)(u64(base) * base % modulus);\nexponent >>= 1;\n}\nreturn result;\n}\ninline u32 find_primitive_root(u32 prime) {\nswitch (prime) {\ncase 998244353U: return 3U;\ncase 754974721U: return 11U;\ncase 167772161U: return 3U;\ncase 469762049U: return 3U;\ndefault: break;\n}\nu32 factors[16];\nint factor_count = 0;\nu32 remaining = prime - 1;\nfor (u32 divisor = 2; u64(divisor) * divisor <= remaining; ++divisor) {\nif (remaining % divisor != 0) continue;\nfactors[factor_count++] = divisor;\ndo {\nremaining /= divisor;\n} while (remaining % divisor == 0);\n}\nif (remaining != 1) factors[factor_count++] = remaining;\nfor (u32 candidate = 2;; ++candidate) {\nbool valid = true;\nfor (int i = 0; i < factor_count; ++i) {\nif (power_mod(candidate, (prime - 1) / factors[i]) == 1) {\nvalid = false;\nbreak;\n}\n}\nif (valid) return candidate;\n}\n}\ninline V shrink(V value) {\nconst V mod = _mm256_set1_epi32((int)(modulus));\nreturn _mm256_min_epu32(value, _mm256_sub_epi32(value, mod));\n}\ninline V shrink_twice_modulus(V value) {\nconst V twice_modulus = _mm256_set1_epi32(\n(int)(2U * modulus));\nreturn _mm256_min_epu32(\nvalue, _mm256_sub_epi32(value, twice_modulus));\n}\ninline V add_lazy(V a, V b) {\nreturn _mm256_add_epi32(a, b);\n}\ninline V subtract_lazy(V a, V b) {\nconst V twice_modulus = _mm256_set1_epi32(\n(int)(2U * modulus));\nreturn _mm256_sub_epi32(_mm256_add_epi32(a, twice_modulus), b);\n}\ninline V add_mod(V a, V b) {\nreturn shrink(_mm256_add_epi32(a, b));\n}\ninline V subtract_mod(V a, V b) {\nconst V mod = _mm256_set1_epi32((int)(modulus));\nreturn shrink(_mm256_sub_epi32(_mm256_add_epi32(a, mod), b));\n}\ninline V montgomery_multiply_lazy(\nV a, V b, const Montgomery& montgomery) {\nconst V inverse = _mm256_set1_epi32(\n(int)(montgomery.negative_inverse));\nconst V mod = _mm256_set1_epi32((int)(modulus));\nconst V even_product = _mm256_mul_epu32(a, b);\nconst V odd_product = _mm256_mul_epu32(\n_mm256_srli_epi64(a, 32), _mm256_srli_epi64(b, 32));\nconst V even_correction = _mm256_mul_epu32(even_product, inverse);\nconst V odd_correction = _mm256_mul_epu32(odd_product, inverse);\nconst V even_sum = _mm256_add_epi64(\neven_product, _mm256_mul_epu32(even_correction, mod));\nconst V odd_sum = _mm256_add_epi64(\nodd_product, _mm256_mul_epu32(odd_correction, mod));\nconst V even_result = _mm256_srli_epi64(even_sum, 32);\nconst V odd_result = _mm256_slli_epi64(\n_mm256_srli_epi64(odd_sum, 32), 32);\nreturn _mm256_or_si256(even_result, odd_result);\n}\ninline V montgomery_multiply(\nV a, V b, const Montgomery& montgomery) {\nreturn shrink(montgomery_multiply_lazy(a, b, montgomery));\n}\nclass TransformPlan {\ncZ size_;\nMontgomery montgomery_;\nu32* twiddles_;\nvoid fill_stage(u32* destination, cZ count, u32 ratio) {\nconst u32 ratio_montgomery = montgomery_.to_montgomery(ratio);\nu32 first_powers[8];\nfirst_powers[0] = montgomery_.radix;\nfor (int i = 1; i < 8; ++i) {\nfirst_powers[i] = montgomery_.multiply(\nfirst_powers[i - 1], ratio_montgomery);\n}\nif (count < 8) {\nstd::memcpy(destination, first_powers, count * sizeof(u32));\nreturn;\n}\nV powers = _mm256_loadu_si256(\n(const V*)(first_powers));\nu32 ratio_eighth = first_powers[7];\nratio_eighth = montgomery_.multiply(ratio_eighth, ratio_montgomery);\nconst V step = _mm256_set1_epi32((int)(ratio_eighth));\nfor (cZ i = 0; i < count; i += 8) {\n_mm256_storeu_si256(\n(V*)(destination + i), powers);\npowers = montgomery_multiply(powers, step, montgomery_);\n}\n}\nvoid build_twiddles() {\nfor (cZ length = size_;; length >>= 1) {\nconst cZ half = length >> 1;\nconst cZ offset = size_ - length;\nconst u32 root = power_mod(\nprimitive_root,\n(u32)((modulus - 1) / length));\nfill_stage(twiddles_ + offset, half, root);\nif (length == 2) break;\n}\n}\nvoid forward_single(\nu32* data, cZ length, u32* second = nullptr) const {\nconst cZ half = length >> 1;\nconst u32* twiddle = twiddles_ + size_ - length;\nfor (cZ block = 0; block < size_; block += length) {\nfor (cZ j = 0; j < half; j += 8) {\nconst V weight = _mm256_loadu_si256(\n(const V*)(twiddle + j));\nconst auto process = [&](u32* target) {\nconst V left = shrink_twice_modulus(\n_mm256_loadu_si256((const V*)(\ntarget + block + j)));\nconst V right = shrink_twice_modulus(\n_mm256_loadu_si256((const V*)(\ntarget + block + half + j)));\n_mm256_storeu_si256(\n(V*)(target + block + j),\nadd_lazy(left, right));\n_mm256_storeu_si256(\n(V*)(target + block + half + j),\nmontgomery_multiply_lazy(\nsubtract_lazy(left, right), weight, montgomery_));\n};\nprocess(data);\nif (second != nullptr) process(second);\n}\n}\n}\nvoid forward_pair(\nu32* data, cZ length, u32* second = nullptr) const {\nconst cZ quarter = length >> 2;\nconst u32* outer = twiddles_ + size_ - length;\nconst u32* inner = twiddles_ + size_ - (length >> 1);\nfor (cZ block = 0; block < size_; block += length) {\nfor (cZ j = 0; j < quarter; j += 8) {\nconst V outer0 = _mm256_loadu_si256(\n(const V*)(outer + j));\nconst V outer1 = _mm256_loadu_si256(\n(const V*)(outer + quarter + j));\nconst V inner_weight = _mm256_loadu_si256(\n(const V*)(inner + j));\nconst auto process = [&](u32* target) {\nconst V a = shrink_twice_modulus(_mm256_loadu_si256(\n(const V*)(target + block + j)));\nconst V b = shrink_twice_modulus(_mm256_loadu_si256(\n(const V*)(target + block + quarter + j)));\nconst V c = shrink_twice_modulus(_mm256_loadu_si256(\n(const V*)(target + block + 2 * quarter + j)));\nconst V d = shrink_twice_modulus(_mm256_loadu_si256(\n(const V*)(target + block + 3 * quarter + j)));\nconst V ac_sum = shrink_twice_modulus(add_lazy(a, c));\nconst V ac_difference = montgomery_multiply_lazy(\nsubtract_lazy(a, c), outer0, montgomery_);\nconst V bd_sum = shrink_twice_modulus(add_lazy(b, d));\nconst V bd_difference = montgomery_multiply_lazy(\nsubtract_lazy(b, d), outer1, montgomery_);\n_mm256_storeu_si256(\n(V*)(target + block + j),\nadd_lazy(ac_sum, bd_sum));\n_mm256_storeu_si256(\n(V*)(target + block + quarter + j),\nmontgomery_multiply_lazy(\nsubtract_lazy(ac_sum, bd_sum), inner_weight, montgomery_));\n_mm256_storeu_si256(\n(V*)(target + block + 2 * quarter + j),\nadd_lazy(ac_difference, bd_difference));\n_mm256_storeu_si256(\n(V*)(target + block + 3 * quarter + j),\nmontgomery_multiply_lazy(\nsubtract_lazy(ac_difference, bd_difference),\ninner_weight, montgomery_));\n};\nprocess(data);\nif (second != nullptr) process(second);\n}\n}\n}\nvoid forward_pair_half_zero(u32* data, u32* second = nullptr) const {\nconst cZ quarter = size_ >> 2;\nconst u32* outer = twiddles_;\nconst u32* inner = twiddles_ + (size_ >> 1);\nfor (cZ j = 0; j < quarter; j += 8) {\nconst V outer0 = _mm256_loadu_si256(\n(const V*)(outer + j));\nconst V outer1 = _mm256_loadu_si256(\n(const V*)(outer + quarter + j));\nconst V inner_weight = _mm256_loadu_si256(\n(const V*)(inner + j));\nconst auto process = [&](u32* target) {\nconst V a = _mm256_loadu_si256(\n(const V*)(target + j));\nconst V b = _mm256_loadu_si256(\n(const V*)(target + quarter + j));\nconst V first = montgomery_multiply_lazy(\na, outer0, montgomery_);\nconst V second_value = montgomery_multiply_lazy(\nb, outer1, montgomery_);\n_mm256_storeu_si256(\n(V*)(target + j), add_lazy(a, b));\n_mm256_storeu_si256(\n(V*)(target + quarter + j),\nmontgomery_multiply_lazy(\nsubtract_lazy(a, b), inner_weight, montgomery_));\n_mm256_storeu_si256(\n(V*)(target + 2 * quarter + j),\nadd_lazy(first, second_value));\n_mm256_storeu_si256(\n(V*)(target + 3 * quarter + j),\nmontgomery_multiply_lazy(\nsubtract_lazy(first, second_value),\ninner_weight, montgomery_));\n};\nprocess(data);\nif (second != nullptr) process(second);\n}\n}\nvoid forward_single_half_zero(u32* data, u32* second = nullptr) const {\nconst cZ half = size_ >> 1;\nfor (cZ j = 0; j < half; j += 8) {\nconst V value = _mm256_loadu_si256(\n(const V*)(data + j));\nconst V weight = _mm256_loadu_si256(\n(const V*)(twiddles_ + j));\n_mm256_storeu_si256(\n(V*)(data + half + j),\nmontgomery_multiply_lazy(value, weight, montgomery_));\nif (second != nullptr) {\nconst V second_value = _mm256_loadu_si256(\n(const V*)(second + j));\n_mm256_storeu_si256(\n(V*)(second + half + j),\nmontgomery_multiply_lazy(\nsecond_value, weight, montgomery_));\n}\n}\n}\nvoid forward_bottom8(u32* data, u32* product = nullptr) const {\nconst u32* twiddle8 = twiddles_ + size_ - 8;\nconst u32* twiddle4 = twiddles_ + size_ - 4;\nconst __m128i w8_low = _mm_loadu_si128(\nreinterpret_cast<const __m128i*>(twiddle8));\nconst V w8 = _mm256_broadcastsi128_si256(w8_low);\nconst V w4 = _mm256_setr_epi32(\ntwiddle4[0], twiddle4[1], twiddle4[0], twiddle4[1],\ntwiddle4[0], twiddle4[1], twiddle4[0], twiddle4[1]);\nfor (cZ block = 0; block < size_; block += 8) {\nV value = shrink(shrink_twice_modulus(_mm256_loadu_si256(\n(const V*)(data + block))));\nV other = _mm256_permute2x128_si256(value, value, 1);\nV sum = add_mod(value, other);\nV difference = montgomery_multiply(\nsubtract_mod(value, other), w8, montgomery_);\nvalue = _mm256_blend_epi32(\nsum, _mm256_permute2x128_si256(difference, difference, 1), 0xF0);\nother = _mm256_shuffle_epi32(value, 0x4E);\nsum = add_mod(value, other);\ndifference = montgomery_multiply(\nsubtract_mod(value, other), w4, montgomery_);\nvalue = _mm256_blend_epi32(\nsum, _mm256_shuffle_epi32(difference, 0x4E), 0xCC);\nother = _mm256_shuffle_epi32(value, 0xB1);\nsum = add_mod(value, other);\ndifference = subtract_mod(value, other);\nvalue = _mm256_blend_epi32(\nsum, _mm256_shuffle_epi32(difference, 0xB1), 0xAA);\nif (product == nullptr) {\n_mm256_storeu_si256(\n(V*)(data + block), value);\n} else {\nconst V other_transform = _mm256_loadu_si256(\n(const V*)(product + block));\n_mm256_storeu_si256(\n(V*)(product + block),\nmontgomery_multiply(\nother_transform, value, montgomery_));\n}\n}\n}\nvoid inverse_single(\nu32* data, cZ length, bool canonicalize = false) const {\nconst cZ half = length >> 1;\nconst u32* twiddle = twiddles_ + size_ - length;\nfor (cZ block = 0; block < size_; block += length) {\nfor (cZ j = 0; j < half; j += 8) {\nconst V left = shrink_twice_modulus(_mm256_loadu_si256(\n(const V*)(data + block + j)));\nconst V right = montgomery_multiply_lazy(\nshrink_twice_modulus(_mm256_loadu_si256(\n(const V*)(data + block + half + j))),\n_mm256_loadu_si256((const V*)(\ntwiddle + j)),\nmontgomery_);\nV sum = add_lazy(left, right);\nV difference = subtract_lazy(left, right);\nif (canonicalize) {\nsum = shrink(shrink_twice_modulus(sum));\ndifference = shrink(shrink_twice_modulus(difference));\n}\n_mm256_storeu_si256(\n(V*)(data + block + j), sum);\n_mm256_storeu_si256(\n(V*)(data + block + half + j),\ndifference);\n}\n}\n}\nvoid inverse_pair(u32* data, cZ length) const {\nconst cZ quarter = length >> 2;\nconst u32* outer = twiddles_ + size_ - length;\nconst u32* inner = twiddles_ + size_ - (length >> 1);\nfor (cZ block = 0; block < size_; block += length) {\nfor (cZ j = 0; j < quarter; j += 8) {\nconst V a = shrink_twice_modulus(_mm256_loadu_si256(\n(const V*)(data + block + j)));\nconst V b = shrink_twice_modulus(_mm256_loadu_si256(\n(const V*)(data + block + quarter + j)));\nconst V c = shrink_twice_modulus(_mm256_loadu_si256(\n(const V*)(data + block + 2 * quarter + j)));\nconst V d = shrink_twice_modulus(_mm256_loadu_si256(\n(const V*)(data + block + 3 * quarter + j)));\nconst V inner_weight = _mm256_loadu_si256(\n(const V*)(inner + j));\nconst V outer0 = _mm256_loadu_si256(\n(const V*)(outer + j));\nconst V outer1 = _mm256_loadu_si256(\n(const V*)(outer + quarter + j));\nconst V bw = montgomery_multiply_lazy(b, inner_weight, montgomery_);\nconst V dw = montgomery_multiply_lazy(d, inner_weight, montgomery_);\nconst V ab_sum = shrink_twice_modulus(add_lazy(a, bw));\nconst V ab_difference = shrink_twice_modulus(subtract_lazy(a, bw));\nconst V cd_sum = add_lazy(c, dw);\nconst V cd_difference = subtract_lazy(c, dw);\nconst V cd_sum_weighted = montgomery_multiply_lazy(\ncd_sum, outer0, montgomery_);\nconst V cd_difference_weighted = montgomery_multiply_lazy(\ncd_difference, outer1, montgomery_);\nV output0 = add_lazy(ab_sum, cd_sum_weighted);\nV output1 = add_lazy(\nab_difference, cd_difference_weighted);\nV output2 = subtract_lazy(ab_sum, cd_sum_weighted);\nV output3 = subtract_lazy(\nab_difference, cd_difference_weighted);\nif (length == size_) {\noutput0 = shrink(shrink_twice_modulus(output0));\noutput1 = shrink(shrink_twice_modulus(output1));\noutput2 = shrink(shrink_twice_modulus(output2));\noutput3 = shrink(shrink_twice_modulus(output3));\n}\n_mm256_storeu_si256(\n(V*)(data + block + j), output0);\n_mm256_storeu_si256(\n(V*)(data + block + quarter + j),\noutput1);\n_mm256_storeu_si256(\n(V*)(data + block + 2 * quarter + j),\noutput2);\n_mm256_storeu_si256(\n(V*)(data + block + 3 * quarter + j),\noutput3);\n}\n}\n}\nvoid inverse_bottom8(u32* data) const {\nconst u32* twiddle4 = twiddles_ + size_ - 4;\nconst u32* twiddle8 = twiddles_ + size_ - 8;\nconst V w4 = _mm256_setr_epi32(\ntwiddle4[0], twiddle4[1], twiddle4[0], twiddle4[1],\ntwiddle4[0], twiddle4[1], twiddle4[0], twiddle4[1]);\nconst __m128i w8_low = _mm_loadu_si128(\nreinterpret_cast<const __m128i*>(twiddle8));\nconst V w8 = _mm256_broadcastsi128_si256(w8_low);\nfor (cZ block = 0; block < size_; block += 8) {\nV value = _mm256_loadu_si256(\n(const V*)(data + block));\nV other = _mm256_shuffle_epi32(value, 0xB1);\nV sum = add_mod(value, other);\nV difference = subtract_mod(value, other);\nvalue = _mm256_blend_epi32(\nsum, _mm256_shuffle_epi32(difference, 0xB1), 0xAA);\nother = _mm256_shuffle_epi32(value, 0x4E);\nother = montgomery_multiply(other, w4, montgomery_);\nsum = add_mod(value, other);\ndifference = subtract_mod(value, other);\nvalue = _mm256_blend_epi32(\nsum, _mm256_shuffle_epi32(difference, 0x4E), 0xCC);\nother = _mm256_permute2x128_si256(value, value, 1);\nother = montgomery_multiply(other, w8, montgomery_);\nsum = add_mod(value, other);\ndifference = subtract_mod(value, other);\nvalue = _mm256_blend_epi32(\nsum, _mm256_permute2x128_si256(difference, difference, 1), 0xF0);\n_mm256_storeu_si256(\n(V*)(data + block), value);\n}\n}\npublic:\nexplicit TransformPlan(cZ size)\n: size_(size),\ntwiddles_(static_cast<u32*>(\n_mm_malloc(sizeof(u32) * size, 32))) {\nbuild_twiddles();\n}\n~TransformPlan() {\n_mm_free(twiddles_);\n}\nconst Montgomery& montgomery() const { return montgomery_; }\nvoid prepare_inverse() {\nfor (cZ length = size_;; length >>= 1) {\nconst cZ half = length >> 1;\nu32* stage = twiddles_ + size_ - length;\ncZ left = 1;\ncZ right = half - 1;\nwhile (left < right) {\nconst u32 a = stage[left];\nconst u32 b = stage[right];\nstage[left++] = modulus - b;\nstage[right--] = modulus - a;\n}\nif (left == right && left != 0) {\nstage[left] = modulus - stage[left];\n}\nif (length == 2) break;\n}\n}\nvoid forward(u32* data, u32* product = nullptr) const {\ncZ length = size_;\nif ((__builtin_ctzll(size_) & 1) != 0) {\nforward_single(data, length);\nlength >>= 1;\n}\nwhile (length > 16) {\nforward_pair(data, length);\nlength >>= 2;\n}\nforward_single(data, 16);\nforward_bottom8(data, product);\n}\nvoid forward_half_zero(u32* data, u32* product = nullptr) const {\ncZ length;\nif ((__builtin_ctzll(size_) & 1) == 0) {\nforward_pair_half_zero(data);\nlength = size_ >> 2;\n} else {\nforward_single_half_zero(data);\nlength = size_ >> 1;\n}\nwhile (length > 16) {\nforward_pair(data, length);\nlength >>= 2;\n}\nforward_single(data, 16);\nforward_bottom8(data, product);\n}\nvoid inverse(u32* data) const {\ninverse_bottom8(data);\ninverse_single(data, 16);\nconst bool has_unpaired_top = (__builtin_ctzll(size_) & 1) != 0;\nconst cZ paired_limit = has_unpaired_top ? size_ >> 1 : size_;\nfor (cZ length = 64; length <= paired_limit; length <<= 2) {\ninverse_pair(data, length);\n}\nif (has_unpaired_top) inverse_single(data, size_, true);\n}\n};\ninline void convolution_ntt_friendly(\nu32* output,\nconst u32* left,\ncZ left_size,\nconst u32* right,\ncZ right_size,\ncZ transform_size,\nu32 modulus_value,\nu32 primitive_root_value,\nbool montgomery_representation) {\nmodulus = modulus_value;\nprimitive_root = primitive_root_value != 0\n? primitive_root_value : find_primitive_root(modulus_value);\nu32* a = output;\nu32* b = static_cast<u32*>(\n_mm_malloc(sizeof(u32) * transform_size, 32));\nstd::memcpy(a, left, sizeof(u32) * left_size);\nstd::memcpy(b, right, sizeof(u32) * right_size);\nTransformPlan plan(transform_size);\nconst Montgomery& montgomery = plan.montgomery();\nif (montgomery_representation) {\nconst V one = _mm256_set1_epi32(1);\ncZ i = 0;\nfor (; i + 8 <= left_size; i += 8) {\nconst V value = _mm256_loadu_si256(\n(const V*)(a + i));\n_mm256_storeu_si256(\n(V*)(a + i),\nmontgomery_multiply(value, one, montgomery));\n}\nfor (; i < left_size; ++i) a[i] = montgomery.multiply(a[i], 1);\ni = 0;\nfor (; i + 8 <= right_size; i += 8) {\nconst V value = _mm256_loadu_si256(\n(const V*)(b + i));\n_mm256_storeu_si256(\n(V*)(b + i),\nmontgomery_multiply(value, one, montgomery));\n}\nfor (; i < right_size; ++i) b[i] = montgomery.multiply(b[i], 1);\n}\nconst cZ half = transform_size >> 1;\nconst bool left_half_zero = left_size <= half;\nconst bool right_half_zero = right_size <= half;\nstd::memset(a + left_size, 0, sizeof(u32) *\n((left_half_zero ? half : transform_size) - left_size));\nstd::memset(b + right_size, 0, sizeof(u32) *\n((right_half_zero ? half : transform_size) - right_size));\nconst u32 inverse_size = power_mod(\n(u32)(transform_size % modulus), modulus - 2);\nconst u32 scaled_radix_squared = (u32)(\nu64(montgomery.radix_squared) * inverse_size % modulus);\nconst V conversion = _mm256_set1_epi32(\n(int)(scaled_radix_squared));\nconst cZ right_initialized = right_half_zero ? half : transform_size;\nfor (cZ i = 0; i < right_initialized; i += 8) {\nconst V value = _mm256_loadu_si256(\n(const V*)(b + i));\n_mm256_storeu_si256(\n(V*)(b + i),\nmontgomery_multiply(value, conversion, montgomery));\n}\nif (left_half_zero) plan.forward_half_zero(a); else plan.forward(a);\nif (right_half_zero) {\nplan.forward_half_zero(b, a);\n} else {\nplan.forward(b, a);\n}\nplan.prepare_inverse();\nplan.inverse(a);\nif (montgomery_representation) {\nconst cZ output_size = left_size + right_size - 1;\nconst V radix_squared = _mm256_set1_epi32(\n(int)(montgomery.radix_squared));\ncZ i = 0;\nfor (; i + 8 <= output_size; i += 8) {\nconst V value = _mm256_loadu_si256(\n(const V*)(a + i));\n_mm256_storeu_si256(\n(V*)(a + i),\nmontgomery_multiply(value, radix_squared, montgomery));\n}\nfor (; i < output_size; ++i) {\na[i] = montgomery.to_montgomery(a[i]);\n}\n}\n_mm_free(b);\n}\n}  \n#endif\nextern \"C\" void cplib_convolution_ntt_friendly(\nstd::uint32_t* output,\nstd::uint32_t* left,\ncZ left_size,\nstd::uint32_t* right,\ncZ right_size,\ncZ transform_size,\nstd::uint32_t modulus,\nstd::uint32_t primitive_root,\nbool montgomery_representation) {\ncplib_avx2_ntt::convolution_ntt_friendly(\noutput, left, left_size, right, right_size, transform_size,\nmodulus, primitive_root, montgomery_representation);\n}\n    \"\"\".}\n\n    proc convolutionNttFriendlyAvx2(\n        output: ptr uint32,\n        f: ptr uint32,\n        fLen: csize_t,\n        g: ptr uint32,\n        gLen: csize_t,\n        nttLen: csize_t,\n        modulus: uint32,\n        primitiveRoot: uint32,\n        montgomeryRepresentation: bool\n    ) {.importc: \"cplib_convolution_ntt_friendly\".}\n\n    proc convolutionNttFriendlyU32(\n        f, g: seq[uint32], modulus, primitiveRoot: uint32\n    ): seq[uint32]\n\n    proc convolutionArbitraryMod[T: BarrettModint or MontgomeryModint](\n        f, g: seq[T]\n    ): seq[T]\n\n    proc convolution_naive*[T: BarrettModint or MontgomeryModint or int](f, g: seq[T]): seq[T] =\n        if f.len == 0 or g.len == 0: return @[]\n        var ans = newSeq[T](f.len + g.len - 1)\n        if f.len > g.len:\n            for i in 0..<f.len:\n                for j in 0..<g.len:\n                    ans[i+j] += f[i] * g[j]\n        else:\n            for j in 0..<g.len:\n                for i in 0..<f.len:\n                    ans[i+j] += f[i] * g[j]\n        return ans\n\n    proc convolution*[T: BarrettModint or MontgomeryModint](f, g: seq[T]): seq[T] =\n        let m = f.len\n        let n = g.len\n        if m == 0 or n == 0: return @[]\n        let deg = m + n - 1\n        if min(n, m) <= 60: return convolution_naive(f, g)\n        var l = (if deg == 1: 1 else: (1 shl (fastLog2(deg - 1) + 1)))\n        if T.umod < (1u32 shl 30) and\n                (T.umod - 1u32) mod l.uint32 == 0u32:\n            result = newSeq[T](l)\n            convolutionNttFriendlyAvx2(\n                cast[ptr uint32](addr result[0]),\n                cast[ptr uint32](unsafeAddr f[0]), m.csize_t,\n                cast[ptr uint32](unsafeAddr g[0]), n.csize_t,\n                l.csize_t, T.umod, 0u32,\n                T is MontgomeryModint)\n            result.setLen(deg)\n            return\n        return convolutionArbitraryMod(f, g)\n\n    proc convolution*[m: static[int]](f, g: seq[int]): seq[int] =\n        doAssert m > 0 and m < (1 shl 31),\n            \"convolution modulus must be in [1, 2^31)\"\n        if f.len == 0 or g.len == 0: return @[]\n        type Mint = StaticBarrettModint[m.uint32]\n        var fm = newSeq[Mint](f.len)\n        var gm = newSeq[Mint](g.len)\n        for i in 0..<f.len: fm[i] = init(Mint, f[i])\n        for i in 0..<g.len: gm[i] = init(Mint, g[i])\n        let product = convolution(fm, gm)\n        result = newSeq[int](product.len)\n        for i in 0..<product.len: result[i] = product[i].val\n\n    proc convolutionNttFriendlyU32(\n            f, g: seq[uint32], modulus, primitiveRoot: uint32): seq[uint32] =\n        if f.len == 0 or g.len == 0: return @[]\n        if min(f.len, g.len) <= 60:\n            result = newSeq[uint32](f.len + g.len - 1)\n            if f.len > g.len:\n                for i in 0..<f.len:\n                    for j in 0..<g.len:\n                        result[i + j] = ((result[i + j].uint64 +\n                            f[i].uint64 * g[j].uint64) mod modulus.uint64).uint32\n            else:\n                for j in 0..<g.len:\n                    for i in 0..<f.len:\n                        result[i + j] = ((result[i + j].uint64 +\n                            f[i].uint64 * g[j].uint64) mod modulus.uint64).uint32\n            return\n        let deg = f.len + g.len - 1\n        let l = (if deg == 1: 1 else: (1 shl (fastLog2(deg - 1) + 1)))\n        result = newSeq[uint32](l)\n        convolutionNttFriendlyAvx2(\n            addr result[0], cast[ptr uint32](unsafeAddr f[0]), f.len.csize_t,\n            cast[ptr uint32](unsafeAddr g[0]), g.len.csize_t, l.csize_t,\n            modulus, primitiveRoot, false)\n        result.setLen(deg)\n\n    proc convolutionArbitraryMod[T: BarrettModint or MontgomeryModint](\n            f, g: seq[T]): seq[T] =\n        const\n            M1 = 754974721u64\n            M2 = 167772161u64\n            M3 = 469762049u64\n            M12 = M1 * M2\n            InvM1ModM2 = inv_gcd((M1 mod M2).int, M2.int)[1].uint64\n            InvM12ModM3 = inv_gcd((M12 mod M3).int, M3.int)[1].uint64\n\n        # With mod < 2^31 and this transform-size limit, every integer\n        # coefficient is smaller than M1*M2*M3, so the three residues identify\n        # it uniquely before reducing it modulo the requested modulus.\n        let targetMod = T.umod.uint64\n        assert targetMod > 0 and targetMod < (1u64 shl 31),\n            \"arbitrary-mod convolution requires a modulus in [1, 2^31)\"\n        let transformSize = 1 shl (fastLog2(f.len + g.len - 2) + 1)\n        assert transformSize <= (1 shl 24),\n            \"arbitrary-mod convolution requires an NTT length at most 2^24\"\n\n        var fm = newSeq[uint32](f.len)\n        var gm = newSeq[uint32](g.len)\n        for i in 0..<f.len: fm[i] = (f[i].val.uint64 mod M1).uint32\n        for i in 0..<g.len: gm[i] = (g[i].val.uint64 mod M1).uint32\n        let c1 = convolutionNttFriendlyU32(fm, gm, M1.uint32, 11u32)\n\n        for i in 0..<f.len: fm[i] = (f[i].val.uint64 mod M2).uint32\n        for i in 0..<g.len: gm[i] = (g[i].val.uint64 mod M2).uint32\n        let c2 = convolutionNttFriendlyU32(fm, gm, M2.uint32, 3u32)\n\n        for i in 0..<f.len: fm[i] = (f[i].val.uint64 mod M3).uint32\n        for i in 0..<g.len: gm[i] = (g[i].val.uint64 mod M3).uint32\n        let c3 = convolutionNttFriendlyU32(fm, gm, M3.uint32, 3u32)\n\n        let m1Target = M1 mod targetMod\n        let m12Target = M12 mod targetMod\n        result = newSeq[T](c1.len)\n        for i in 0..<result.len:\n            let r1 = c1[i].uint64\n            let t2 = ((c2[i].uint64 + M2 - r1 mod M2) mod M2 *\n                InvM1ModM2) mod M2\n            let r12ModM3 = (r1 + (M1 mod M3) * t2) mod M3\n            let t3 = ((c3[i].uint64 + M3 - r12ModM3) mod M3 *\n                InvM12ModM3) mod M3\n            let value = ((r1 mod targetMod) + m1Target * t2 mod targetMod +\n                m12Target * t3 mod targetMod) mod targetMod\n            result[i] = init(T, value.int)\n\n\n    proc convolution_ll*(f, g: seq[int]): seq[int] =\n        var n = f.len\n        var m = g.len\n        if n == 0 or m == 0: return newSeq[int]()\n\n        const\n            M1 = 754974721u\n            M2 = 167772161u\n            M3 = 469762049u\n            M12 = M1 * M2\n            M23 = M2 * M3\n            M31 = M3 * M1\n            M123 = M1 * M2 * M3\n            i1 = inv_gcd((M2 * M3).int, M1.int)[1].uint\n            i2 = inv_gcd((M3 * M1).int, M2.int)[1].uint\n            i3 = inv_gcd((M1 * M2).int, M3.int)[1].uint\n        var fm = newSeq[uint32](n)\n        var gm = newSeq[uint32](m)\n        for i in 0..<n: fm[i] = floorMod(f[i], M1.int).uint32\n        for i in 0..<m: gm[i] = floorMod(g[i], M1.int).uint32\n        let c1 = convolutionNttFriendlyU32(fm, gm, M1.uint32, 11u32)\n        for i in 0..<n: fm[i] = floorMod(f[i], M2.int).uint32\n        for i in 0..<m: gm[i] = floorMod(g[i], M2.int).uint32\n        let c2 = convolutionNttFriendlyU32(fm, gm, M2.uint32, 3u32)\n        for i in 0..<n: fm[i] = floorMod(f[i], M3.int).uint32\n        for i in 0..<m: gm[i] = floorMod(g[i], M3.int).uint32\n        let c3 = convolutionNttFriendlyU32(fm, gm, M3.uint32, 3u32)\n        var ans = newseqwith(n + m - 1, 0)\n        for i in 0..<ans.len:\n            var x = 0.uint\n            x += (c1[i].uint * i1) mod M1 * M23\n            x += (c2[i].uint * i2) mod M2 * M31\n            x += (c3[i].uint * i3) mod M3 * M12\n            # x intentionally wraps modulo 2^64.  Reinterpret those bits as a\n            # signed value for the CRT overflow correction; a numeric `.int`\n            # conversion raises RangeDefect when the top bit is set.\n            var diff = c1[i].int - floorMod(cast[int](x), M1.int)\n            if diff < 0: diff += M1.int\n            const offset = [0u, 0u, M123, 2u * M123, 3u * M123]\n            x -= offset[diff mod 5]\n            ans[i] = cast[int](x)\n        return ans\n"

{.checks: off.}

var n = input(int)
var g = initUnWeightedUnDirectedStaticGraph(n)
for i in 0..<n-1:
    var u, v = input(int)-1
    g.add_edge(u, v)
g.build
var s = input(string).mapIt(if it == '1': 1 else: -1)

var removed = newSeqWith(n, false)
var sz = newSeqWith(n, 0)

var ans = 0
proc mul(f, g: seq[int]): seq[int] = convolution_ll(f, g)
proc add(f, g: seq[int], fg, gg: int): (seq[int], int) =
    var geta = max(fg, gg)
    var ans = newSeqWith(max(f.len - fg + geta, g.len - gg + geta), 0)
    for i in 0..<f.len: ans[i - fg + geta] += f[i]
    for i in 0..<g.len: ans[i - gg + geta] += g[i]
    return (ans, geta)
proc minus(f: seq[int]): seq[int] = f.mapIt(-it)

proc centroid_decompotision(x: int) =
    proc calc_total_size(u, par: int): int =
        result = 1
        for v in g[u]:
            if v == par or removed[v]: continue
            result += calc_total_size(v, u)
    var total_size = calc_total_size(x, -1)
    var m = (total_size + 1) // 2
    proc find_centroid(u, par: int): (int, int) =
        result = (-1, INFL)
        sz[u] = 1
        for v in g[u]:
            if v == par or removed[v]: continue
            var (c, mx) = find_centroid(v, u)
            sz[u] += sz[v]
            if chmin(result[1], mx): result[0] = c
        if sz[u] >= m and chmin(result[1], sz[u]):
            result[0] = u; result[1] = sz[u]
    var (c, _) = find_centroid(x, -1)
    var f = newSeq[seq[int]]()
    var getas = newSeq[int]()
    for v in g[c]:
        if removed[v]: continue
        proc calc_geta(u, par, x: int): int =
            result = x
            for v in g[u]:
                if v == par or removed[v]: continue
                result.min = calc_geta(v, u, x + s[v])
        var geta = max(0, -calc_geta(v, c, s[v]))
        f.add(newSeqWith(geta+sz[v]+1, 0))
        getas.add(geta)
        proc calc_f(u, par, x: int) =
            f[^1][x+geta] += 1
            for v in g[u]:
                if v == par or removed[v]: continue
                calc_f(v, u, x + s[v])
        calc_f(v, c, s[v])

    var gmax = (if getas.len > 0: getas.max else: 0)
    var total = newSeqWith(gmax+sz[x], 0)
    for i in 0..<f.len:
        for j in 0..<f[i].len:
            if f[i][j] != 0:
                total[j-getas[i]+gmax] += f[i][j]
    for i in gmax+1-s[c]..<total.len: ans += total[i]

    total = mul(total, total)
    gmax *= 2
    for i in 0..<f.len:
        f[i] = mul(f[i], f[i])
        getas[i] *= 2
    for i in 0..<f.len:
        for j in 0..<f[i].len:
            if f[i][j] != 0: total[j-getas[i]+gmax] -= f[i][j]
    for i in gmax+1-s[c]..<total.len: ans += total[i] // 2

    removed[c] = true
    for v in g[c]:
        if removed[v]: continue
        centroid_decompotision(v)
centroid_decompotision(0)
print(ans + s.count(1))
0