Input:
from collections import deque

import numpy as np

import sys

read = sys.stdin.buffer.read

readline = sys.stdin.buffer.readline

readlines = sys.stdin.buffer.readlines

sys.setrecursionlimit(10 ** 7)

N, M = map(int, input().split())

AB = [tuple(map(int, input().split())) for _ in range(N + M - 1)]

e_from = [[] for _ in range(N+1)]

e_to = [[] for _ in range(N+1)]

for a, b in AB:

    e_from[a].append(b)

    e_to[b].append(a)

add = np.zeros(N+1, dtype=int)

rank = [-1]*(N+1)

# まず根を探す

for i in range(1, N+1):

    add[i] = len(e_to[i])

    if len(e_to[i]) == 0:

        root = i

# BFS

rank[root] = 0

node = deque([root])

while node:

    s = node.popleft()

    add[e_from[s]] -= 1

    for t in e_from[s]:

        if add[t] == 0:

            rank[t] = s

            node.append(t)

print(*rank[1:], sep="\n")

from collections import deque

import numpy as np

import sys

input = sys.stdin.readline

N, M = map(int, input().split())

AB = [tuple(map(int, input().split())) for _ in range(N + M - 1)]

e_from = [[] for _ in range(N+1)]

e_to = [[] for _ in range(N+1)]

for a, b in AB:

    e_from[a].append(b)

    e_to[b].append(a)

add = np.zeros(N+1, dtype=int)

rank = [-1]*(N+1)

# まず根を探す

for i in range(1, N+1):

    add[i] = len(e_to[i])

    if len(e_to[i]) == 0:

        root = i

# BFS

rank[root] = 0

node = deque([root])

while node:

    s = node.popleft()

    add[e_from[s]] -= 1

    for t in e_from[s]:

        if add[t] == 0:

            rank[t] = s

            node.append(t)

print(*rank[1:], sep="\n")

from collections import deque
import numpy as np
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**7)
N, M = map(int, input().split())
AB = [tuple(map(int, input().split())) for _ in range(N + M - 1)]
e_from = [[] for _ in range(N + 1)]
e_to = [[] for _ in range(N + 1)]
for a, b in AB:
    e_from[a].append(b)
    e_to[b].append(a)
add = np.zeros(N + 1, dtype=int)
rank = [-1] * (N + 1)
# まず根を探す
for i in range(1, N + 1):
    add[i] = len(e_to[i])
    if len(e_to[i]) == 0:
        root = i
# BFS
rank[root] = 0
node = deque([root])
while node:
    s = node.popleft()
    add[e_from[s]] -= 1
    for t in e_from[s]:
        if add[t] == 0:
            rank[t] = s
            node.append(t)
print(*rank[1:], sep="\n")

from collections import deque
import numpy as np
import sys

input = sys.stdin.readline
N, M = map(int, input().split())
AB = [tuple(map(int, input().split())) for _ in range(N + M - 1)]
e_from = [[] for _ in range(N + 1)]
e_to = [[] for _ in range(N + 1)]
for a, b in AB:
    e_from[a].append(b)
    e_to[b].append(a)
add = np.zeros(N + 1, dtype=int)
rank = [-1] * (N + 1)
# まず根を探す
for i in range(1, N + 1):
    add[i] = len(e_to[i])
    if len(e_to[i]) == 0:
        root = i
# BFS
rank[root] = 0
node = deque([root])
while node:
    s = node.popleft()
    add[e_from[s]] -= 1
    for t in e_from[s]:
        if add[t] == 0:
            rank[t] = s
            node.append(t)
print(*rank[1:], sep="\n")

-read = sys.stdin.buffer.read
-readline = sys.stdin.buffer.readline
-readlines = sys.stdin.buffer.readlines
-sys.setrecursionlimit(10**7)
+input = sys.stdin.readline


Input:
from collections import deque

N, M = list(map(int, input().split()))

AB = [list(map(int, input().split())) for _ in range(M)]

#print(AB)

links = [[] for _ in range(N + 1)]

for a, b in AB:

    links[a].append(b)

    links[b].append(a)

#print(AB)

#print(links)

result = [-1] * (N + 1)

q = deque([1])

#print(q)

while q:

    i = q.popleft()

    for j in links[i]:

        if result[j] == -1:

            result[j] = i

            q.append(j)

            #print(q)

print('Yes')

print(('\n'.join(str(i) for i in result[2:])))

from collections import deque

N, M = list(map(int, input().split()))

A = [0] * M

B = [0] * M

for i in range(M):

    A[i], B[i]= list(map(int, input().split()))

links = [[] for _ in range(N + 1)]

for i in range(len(A)):

    links[A[i]].append(B[i])

    links[B[i]].append(A[i])

#print(links)

result = [-1] * (N + 1)

q = deque([1])

#print(q)

while q:

    i = q.popleft()

    for j in links[i]:

        if result[j] == -1:

            result[j] = i

            q.append(j)

            #print(q)

print('Yes')

print(('\n'.join(str(i) for i in result[2:])))

from collections import deque

N, M = list(map(int, input().split()))
AB = [list(map(int, input().split())) for _ in range(M)]
# print(AB)
links = [[] for _ in range(N + 1)]
for a, b in AB:
    links[a].append(b)
    links[b].append(a)
# print(AB)
# print(links)
result = [-1] * (N + 1)
q = deque([1])
# print(q)
while q:
    i = q.popleft()
    for j in links[i]:
        if result[j] == -1:
            result[j] = i
            q.append(j)
            # print(q)
print("Yes")
print(("\n".join(str(i) for i in result[2:])))

from collections import deque

N, M = list(map(int, input().split()))
A = [0] * M
B = [0] * M
for i in range(M):
    A[i], B[i] = list(map(int, input().split()))
links = [[] for _ in range(N + 1)]
for i in range(len(A)):
    links[A[i]].append(B[i])
    links[B[i]].append(A[i])
# print(links)
result = [-1] * (N + 1)
q = deque([1])
# print(q)
while q:
    i = q.popleft()
    for j in links[i]:
        if result[j] == -1:
            result[j] = i
            q.append(j)
            # print(q)
print("Yes")
print(("\n".join(str(i) for i in result[2:])))

-AB = [list(map(int, input().split())) for _ in range(M)]

+    A[i], B[i] = list(map(int, input().split()))


Input:
n, l = list(map(int, input().split()))

s = [eval(input()) for _ in range(n)]

s.sort()

print(("".join(s)))

n, l = list(map(int, input().split()))

s = [eval(input()) for _ in range(n)]

s.sort()

ans = ""

for i in s:

    ans += i

print(ans)

-print(("".join(s)))
+ans = ""
+for i in s:
+    ans += i
+print(ans)


Input:
def main():

    from collections import deque

    N, X, Y = map(int, input().split())

    X -= 1

    Y -= 1

    g = tuple(set() for _ in range(N))

    for v in range(N):

        if v > 0:

            g[v].add(v - 1)

        if v < N - 1:

            g[v].add(v + 1)

    g[X].add(Y)

    g[Y].add(X)

    ctr = [0] * N

    for v in range(N):

        dist = [-1] * N

        q = deque()

        q.append((v, 0))

        while q:

            v, d = q.popleft()

            if ~dist[v] and dist[v] < d: continue

            dist[v] = d

            for u in g[v]:

                if ~dist[u]: continue

                dist[u] = d + 1

                q.append((u, d + 1))

        for d in dist:

            ctr[d] += 1

    print(*map(lambda x: x // 2, ctr[1:]), sep='\n')

if __name__ == '__main__':

    main()

# https://atcoder.jp/contests/abc160/submissions/11310424

def main():

    N, X, Y = map(int, input().split())

    X -= 1

    Y -= 1

    def dist(a, b):

        return min(

            abs(a - b),

            abs(a - X) + abs(b - Y) + 1,

            abs(a - Y) + abs(b - X) + 1

        )

    ctr = [0] * N

    for b in range(N):

        for a in range(b):

            d = dist(a, b)

            ctr[d] += 1

    print(*ctr[1:], sep='\n')

if __name__ == '__main__':

    main()

def main():
    from collections import deque

    N, X, Y = map(int, input().split())
    X -= 1
    Y -= 1
    g = tuple(set() for _ in range(N))
    for v in range(N):
        if v > 0:
            g[v].add(v - 1)
        if v < N - 1:
            g[v].add(v + 1)
    g[X].add(Y)
    g[Y].add(X)
    ctr = [0] * N
    for v in range(N):
        dist = [-1] * N
        q = deque()
        q.append((v, 0))
        while q:
            v, d = q.popleft()
            if ~dist[v] and dist[v] < d:
                continue
            dist[v] = d
            for u in g[v]:
                if ~dist[u]:
                    continue
                dist[u] = d + 1
                q.append((u, d + 1))
        for d in dist:
            ctr[d] += 1
    print(*map(lambda x: x // 2, ctr[1:]), sep="\n")

if __name__ == "__main__":
    main()

# https://atcoder.jp/contests/abc160/submissions/11310424
def main():
    N, X, Y = map(int, input().split())
    X -= 1
    Y -= 1

    def dist(a, b):
        return min(abs(a - b), abs(a - X) + abs(b - Y) + 1, abs(a - Y) + abs(b - X) + 1)

    ctr = [0] * N
    for b in range(N):
        for a in range(b):
            d = dist(a, b)
            ctr[d] += 1
    print(*ctr[1:], sep="\n")

if __name__ == "__main__":
    main()

+        return min(abs(a - b), abs(a - X) + abs(b - Y) + 1, abs(a - Y) + abs(b - X) + 1)

-            if ~dist[v] and dist[v] < d:

-    print(*map(lambda x: x // 2, ctr[1:]), sep="\n")


Input:
# -*- coding: utf-8 -*-

# AtCoder Beginner Contest

# Problem C

if __name__ == '__main__':

    from collections import Counter

    ball_count, kind_count = list(map(int, input().split()))

    ball_numbers = Counter(list(map(int, input().split())))

    sorted_ball_numbers = sorted(list(ball_numbers.items()), key=lambda x: x[1])

    ball_number_key_count = len(list(ball_numbers.keys()))

    need_rewritten_count = 0

    diff_count = ball_number_key_count - kind_count

    if diff_count > 0:

        for i, (key, value) in enumerate(sorted_ball_numbers):

            if diff_count > i:

                need_rewritten_count += value

    print(need_rewritten_count)

# -*- coding: utf-8 -*-

# AtCoder Beginner Contest

# Problem C

if __name__ == '__main__':

    from collections import Counter

    ball_count, kind_count = list(map(int, input().split()))

    ball_numbers = Counter(list(map(int, input().split())))

    sorted_ball_numbers = sorted(ball_numbers.values())

    print((sum(sorted_ball_numbers[:-kind_count])))

# -*- coding: utf-8 -*-
# AtCoder Beginner Contest
# Problem C
if __name__ == "__main__":
    from collections import Counter

    ball_count, kind_count = list(map(int, input().split()))
    ball_numbers = Counter(list(map(int, input().split())))
    sorted_ball_numbers = sorted(list(ball_numbers.items()), key=lambda x: x[1])
    ball_number_key_count = len(list(ball_numbers.keys()))
    need_rewritten_count = 0
    diff_count = ball_number_key_count - kind_count
    if diff_count > 0:
        for i, (key, value) in enumerate(sorted_ball_numbers):
            if diff_count > i:
                need_rewritten_count += value
    print(need_rewritten_count)

# -*- coding: utf-8 -*-
# AtCoder Beginner Contest
# Problem C
if __name__ == "__main__":
    from collections import Counter

    ball_count, kind_count = list(map(int, input().split()))
    ball_numbers = Counter(list(map(int, input().split())))
    sorted_ball_numbers = sorted(ball_numbers.values())
    print((sum(sorted_ball_numbers[:-kind_count])))

-    sorted_ball_numbers = sorted(list(ball_numbers.items()), key=lambda x: x[1])

-    ball_number_key_count = len(list(ball_numbers.keys()))

-    diff_count = ball_number_key_count - kind_count

-        for i, (key, value) in enumerate(sorted_ball_numbers):

-                need_rewritten_count += value

+    sorted_ball_numbers = sorted(ball_numbers.values())

+    print((sum(sorted_ball_numbers[:-kind_count])))


Input:
S = eval(input())

T = eval(input())

cnt, i = 0, 0

for s in S:

    if s == T[i]:

        cnt += 1

    i += 1

print(cnt)

S = eval(input())

T = eval(input())

count = 0

for s,t in zip(S,T):

    if s == t:

        count+=1

print(count)

S = eval(input())
T = eval(input())
count = 0
for s, t in zip(S, T):
    if s == t:
        count += 1
print(count)

-cnt, i = 0, 0
-for s in S:
-    if s == T[i]:
-        cnt += 1
-    i += 1
-print(cnt)
+count = 0
+for s, t in zip(S, T):
+    if s == t:
+        count += 1
+print(count)


Input:
A = eval(input())

print((A[0]+str(len(A[1:-1]))+A[-1]))


Input:
N,D = list(map(int,input().split()))

X = [tuple(map(int,input().split())) for i in range(N)]

ans = 0

for i,a in enumerate(X[:-1]):

    for b in X[i+1:]:

        d = 0

        for x,y in zip(a,b):

            d += (x-y)**2

        if d**0.5 - int(d**0.5) < 10e-10:

            ans += 1

print(ans)

N,D = list(map(int,input().split()))

X = [tuple(map(int,input().split())) for i in range(N)]

ans = 0

for i,x in enumerate(X[:-1]):

    for y in X[i+1:]:

        d = 0

        for a,b in zip(x,y):

            d += (a-b)**2

        if int(d**0.5) == d**0.5:

            ans += 1

print(ans)

N, D = list(map(int, input().split()))
X = [tuple(map(int, input().split())) for i in range(N)]
ans = 0
for i, a in enumerate(X[:-1]):
    for b in X[i + 1 :]:
        d = 0
        for x, y in zip(a, b):
            d += (x - y) ** 2
        if d**0.5 - int(d**0.5) < 10e-10:
            ans += 1
print(ans)

N, D = list(map(int, input().split()))
X = [tuple(map(int, input().split())) for i in range(N)]
ans = 0
for i, x in enumerate(X[:-1]):
    for y in X[i + 1 :]:
        d = 0
        for a, b in zip(x, y):
            d += (a - b) ** 2
        if int(d**0.5) == d**0.5:
            ans += 1
print(ans)


Input:
h, a = list(map(int, input().split()))

if h % a == 0:

  print((h//a))

else:

  print((h//a + 1))

a, b = list(map(int, input().split()))

print((-(-a//b)))

h, a = list(map(int, input().split()))
if h % a == 0:
    print((h // a))
else:
    print((h // a + 1))

a, b = list(map(int, input().split()))
print((-(-a // b)))

-h, a = list(map(int, input().split()))
-if h % a == 0:
-    print((h // a))
-else:
-    print((h // a + 1))
+a, b = list(map(int, input().split()))
+print((-(-a // b)))


Input:
import sys

sys.setrecursionlimit(10000000)

MOD = 998244353

INF = 10 ** 15

def main():

    N,S = list(map(int,input().split()))

    A = list(map(int,input().split()))

    dp = [0] * (1 + S)

    dp[0] = pow(2,N,MOD)

    inv2 = pow(2,MOD - 2,MOD)

    for a in A:

        for s in range(S,a - 1,-1):

            dp[s] = (dp[s] + dp[s - a] * inv2)%MOD

    print((dp[S]))

if __name__ == '__main__':

    main()

import sys

sys.setrecursionlimit(10000000)

MOD = 998244353

INF = 10 ** 15

import numpy as np

def main():

    N,S = list(map(int,input().split()))

    A = list(map(int,input().split()))

    dp = np.zeros(S + 1,np.int64)

    dp[0] = pow(2,N,MOD)

    inv2 = pow(2,MOD - 2,MOD)

    for a in A:

        dp[a:] = np.mod(dp[a:] + dp[:-a] * inv2,MOD)

    print((dp[S]))

if __name__ == '__main__':

    main()

import sys

sys.setrecursionlimit(10000000)
MOD = 998244353
INF = 10**15

def main():
    N, S = list(map(int, input().split()))
    A = list(map(int, input().split()))
    dp = [0] * (1 + S)
    dp[0] = pow(2, N, MOD)
    inv2 = pow(2, MOD - 2, MOD)
    for a in A:
        for s in range(S, a - 1, -1):
            dp[s] = (dp[s] + dp[s - a] * inv2) % MOD
    print((dp[S]))

if __name__ == "__main__":
    main()

import sys

sys.setrecursionlimit(10000000)
MOD = 998244353
INF = 10**15
import numpy as np

def main():
    N, S = list(map(int, input().split()))
    A = list(map(int, input().split()))
    dp = np.zeros(S + 1, np.int64)
    dp[0] = pow(2, N, MOD)
    inv2 = pow(2, MOD - 2, MOD)
    for a in A:
        dp[a:] = np.mod(dp[a:] + dp[:-a] * inv2, MOD)
    print((dp[S]))

if __name__ == "__main__":
    main()

-            dp[s] = (dp[s] + dp[s - a] * inv2) % MOD

+        dp[a:] = np.mod(dp[a:] + dp[:-a] * inv2, MOD)


Input:
#一次独立：rankが成分数

import numpy as np

#0-indexed,MSBがma以下の時

def msb(x,ma=59):

    for i in range(ma,-1,-1):

        if (x>>i)&1:

            return i

    return len()

t=int(eval(input()))

for _ in range(t):

    n=int(eval(input()))

    a=list(map(int,input().split()))

    s=eval(input())

    #独立、msb同じものはない

    #逆に異なれば独立

    msbs=[0 for i in range(60)]

    for i in range(n-1,-1,-1):

        now=a[i]

        m=59

        #独立のときfはTrue、独立でない時fはFalse

        while True:

            m=msb(now,m)

            if msbs[m]==0:

                f=True

                break

            now=msbs[m]^now

            if now==0:

                f=False

                break

        if s[i]=="0":

            if f:

                msbs[m]=now

        else:

            if f:

                print((1))

                break

    else:

        print((0))

t=int(eval(input()))

for _ in range(t):

    n=int(eval(input()))

    a=list(map(int,input().split()))

    s=eval(input())

    b=[]

    for i in range(n)[::-1]:

        for e in b:

            a[i]=min(a[i],a[i]^e)

        if a[i]:

            if s[i]=="0":

                b+=[a[i]]

            else:

                print((1))

                break

    else:

        print((0))

# 一次独立：rankが成分数
import numpy as np

# 0-indexed,MSBがma以下の時
def msb(x, ma=59):
    for i in range(ma, -1, -1):
        if (x >> i) & 1:
            return i
    return len()

t = int(eval(input()))
for _ in range(t):
    n = int(eval(input()))
    a = list(map(int, input().split()))
    s = eval(input())
    # 独立、msb同じものはない
    # 逆に異なれば独立
    msbs = [0 for i in range(60)]
    for i in range(n - 1, -1, -1):
        now = a[i]
        m = 59
        # 独立のときfはTrue、独立でない時fはFalse
        while True:
            m = msb(now, m)
            if msbs[m] == 0:
                f = True
                break
            now = msbs[m] ^ now
            if now == 0:
                f = False
                break
        if s[i] == "0":
            if f:
                msbs[m] = now
        else:
            if f:
                print((1))
                break
    else:
        print((0))

t = int(eval(input()))
for _ in range(t):
    n = int(eval(input()))
    a = list(map(int, input().split()))
    s = eval(input())
    b = []
    for i in range(n)[::-1]:
        for e in b:
            a[i] = min(a[i], a[i] ^ e)
        if a[i]:
            if s[i] == "0":
                b += [a[i]]
            else:
                print((1))
                break
    else:
        print((0))

-# 一次独立：rankが成分数
-import numpy as np
-# 0-indexed,MSBがma以下の時
-def msb(x, ma=59):
-    for i in range(ma, -1, -1):
-        if (x >> i) & 1:
-            return i
-    return len()
-    # 独立、msb同じものはない
-    # 逆に異なれば独立
-    msbs = [0 for i in range(60)]
-    for i in range(n - 1, -1, -1):
-        now = a[i]
-        m = 59
-        # 独立のときfはTrue、独立でない時fはFalse
-        while True:
-            m = msb(now, m)
-            if msbs[m] == 0:
-                f = True
-                break
-            now = msbs[m] ^ now
-            if now == 0:
-                f = False
-                break
-        if s[i] == "0":
-            if f:
-                msbs[m] = now
-        else:
-            if f:
+    b = []
+    for i in range(n)[::-1]:
+        for e in b:
+            a[i] = min(a[i], a[i] ^ e)
+        if a[i]:
+            if s[i] == "0":
+                b += [a[i]]
+            else:


Input:
from collections import defaultdict, deque, Counter

from heapq import heappush, heappop, heapify

import math

import bisect

import random

from itertools import permutations, accumulate, combinations, product

import sys

import string

from bisect import bisect_left, bisect_right

from math import factorial, ceil, floor

from operator import mul

from functools import reduce

sys.setrecursionlimit(2147483647)

INF = 10 ** 20

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

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

def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()

def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')

def IR(n): return [I() for i in range(n)]

def LIR(n): return [LI() for i in range(n)]

def SR(n): return [S() for i in range(n)]

def LSR(n): return [LS() for i in range(n)]

def SRL(n): return [list(S()) for i in range(n)]

def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]

mod = 1000000007

def mat_mul(a, b) :

    I, J, K = len(a), len(b[0]), len(b)

    c = [[0] * J for _ in range(I)]

    for i in range(I) :

        for j in range(J) :

            for k in range(K) :

                c[i][j] += a[i][k] * b[k][j]

            c[i][j] %= m

    return c

def mat_pow(x, n):

    y = [[0] * len(x) for _ in range(len(x))]

    for i in range(len(x)):

        y[i][i] = 1

    while n > 0:

        if n & 1:

            y = mat_mul(x, y)

        x = mat_mul(x, x)

        n >>= 1

    return y

l, a, b, m = LI()

d0 = 0

ret = [[0], [a], [1]]

for i in range(1, 19):

    if 10 ** i - 1 - a < 0:

        continue

    d1 = min((10 ** i - 1 - a) // b + 1, l)

    mat = [[10 ** i, 1, 0], [0, 1, b], [0, 0, 1]]

    ret = mat_mul(mat_pow(mat, d1 - d0), ret)

    if d1 == l:

        break

    d0 = d1

print((ret[0][0]))

from collections import defaultdict, deque, Counter

from heapq import heappush, heappop, heapify

import math

import bisect

import random

from itertools import permutations, accumulate, combinations, product

import sys

import string

from bisect import bisect_left, bisect_right

from math import factorial, ceil, floor

from operator import mul

from functools import reduce

sys.setrecursionlimit(2147483647)

INF = 10 ** 20

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

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

def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()

def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')

def IR(n): return [I() for i in range(n)]

def LIR(n): return [LI() for i in range(n)]

def SR(n): return [S() for i in range(n)]

def LSR(n): return [LS() for i in range(n)]

def SRL(n): return [list(S()) for i in range(n)]

def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]

mod = 1000000007

def mat_mul(a, b) :

    I, J, K = len(a), len(b[0]), len(b)

    c = [[0] * J for _ in range(I)]

    for i in range(I) :

        for j in range(J) :

            for k in range(K) :

                c[i][j] += a[i][k] * b[k][j]

            c[i][j] %= m

    return c

def mat_pow(x, n):

    y = [[0] * len(x) for _ in range(len(x))]

    for i in range(len(x)):

        y[i][i] = 1

    while n > 0:

        if n & 1:

            y = mat_mul(x, y)

        x = mat_mul(x, x)

        n >>= 1

    return y

l, a, b, m = LI()

d0 = 0

ret = [[0], [a], [b]]

for i in range(1, 19):

    if 10 ** i - 1 - a < 0:

        continue

    d1 = min((10 ** i - 1 - a) // b + 1, l)

    mat = [[10 ** i, 1, 0], [0, 1, 1], [0, 0, 1]]

    ret = mat_mul(mat_pow(mat, d1 - d0), ret)

    if d1 == l:

        break

    d0 = d1

print((ret[0][0]))

from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce

sys.setrecursionlimit(2147483647)
INF = 10**20

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

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

def LS():
    return sys.stdin.buffer.readline().rstrip().decode("utf-8").split()

def S():
    return sys.stdin.buffer.readline().rstrip().decode("utf-8")

def IR(n):
    return [I() for i in range(n)]

def LIR(n):
    return [LI() for i in range(n)]

def SR(n):
    return [S() for i in range(n)]

def LSR(n):
    return [LS() for i in range(n)]

def SRL(n):
    return [list(S()) for i in range(n)]

def MSRL(n):
    return [[int(j) for j in list(S())] for i in range(n)]

mod = 1000000007

def mat_mul(a, b):
    I, J, K = len(a), len(b[0]), len(b)
    c = [[0] * J for _ in range(I)]
    for i in range(I):
        for j in range(J):
            for k in range(K):
                c[i][j] += a[i][k] * b[k][j]
            c[i][j] %= m
    return c

def mat_pow(x, n):
    y = [[0] * len(x) for _ in range(len(x))]
    for i in range(len(x)):
        y[i][i] = 1
    while n > 0:
        if n & 1:
            y = mat_mul(x, y)
        x = mat_mul(x, x)
        n >>= 1
    return y

l, a, b, m = LI()
d0 = 0
ret = [[0], [a], [1]]
for i in range(1, 19):
    if 10**i - 1 - a < 0:
        continue
    d1 = min((10**i - 1 - a) // b + 1, l)
    mat = [[10**i, 1, 0], [0, 1, b], [0, 0, 1]]
    ret = mat_mul(mat_pow(mat, d1 - d0), ret)
    if d1 == l:
        break
    d0 = d1
print((ret[0][0]))

from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce

sys.setrecursionlimit(2147483647)
INF = 10**20

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

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

def LS():
    return sys.stdin.buffer.readline().rstrip().decode("utf-8").split()

def S():
    return sys.stdin.buffer.readline().rstrip().decode("utf-8")

def IR(n):
    return [I() for i in range(n)]

def LIR(n):
    return [LI() for i in range(n)]

def SR(n):
    return [S() for i in range(n)]

def LSR(n):
    return [LS() for i in range(n)]

def SRL(n):
    return [list(S()) for i in range(n)]

def MSRL(n):
    return [[int(j) for j in list(S())] for i in range(n)]

mod = 1000000007

def mat_mul(a, b):
    I, J, K = len(a), len(b[0]), len(b)
    c = [[0] * J for _ in range(I)]
    for i in range(I):
        for j in range(J):
            for k in range(K):
                c[i][j] += a[i][k] * b[k][j]
            c[i][j] %= m
    return c

def mat_pow(x, n):
    y = [[0] * len(x) for _ in range(len(x))]
    for i in range(len(x)):
        y[i][i] = 1
    while n > 0:
        if n & 1:
            y = mat_mul(x, y)
        x = mat_mul(x, x)
        n >>= 1
    return y

l, a, b, m = LI()
d0 = 0
ret = [[0], [a], [b]]
for i in range(1, 19):
    if 10**i - 1 - a < 0:
        continue
    d1 = min((10**i - 1 - a) // b + 1, l)
    mat = [[10**i, 1, 0], [0, 1, 1], [0, 0, 1]]
    ret = mat_mul(mat_pow(mat, d1 - d0), ret)
    if d1 == l:
        break
    d0 = d1
print((ret[0][0]))


Input:
from collections import Counter

n = int(eval(input()))

a = [int(i) for i in input().split()]

cum = [0] * (n + 1)

for i in range(n):

    cum[i + 1] = cum[i] + a[i]

cum = sorted(cum)

count = Counter(cum)

ans = 0

for v in list(count.values()):

    ans += v * (v - 1) // 2

print(ans)

from collections import Counter

N = int(eval(input()))

A = [int(i) for i in input().split()]

cum = [0]

for a in A:

    cum.append(cum[-1] + a)

count = Counter(cum)

ans = 0

for v in list(count.values()):

    ans += v * (v - 1) // 2

print(ans)

-n = int(eval(input()))
-a = [int(i) for i in input().split()]
-cum = [0] * (n + 1)
-for i in range(n):
-    cum[i + 1] = cum[i] + a[i]
-cum = sorted(cum)
+N = int(eval(input()))
+A = [int(i) for i in input().split()]
+cum = [0]
+for a in A:
+    cum.append(cum[-1] + a)


Input:
from fractions import gcd

N,M=list(map(int,input().split()))

S=list(eval(input()))

T=list(eval(input()))

L=gcd(N,M)

print((N*M//L if all([S[i*N//L]==T[i*M//L] for i in range(L)]) else "-1"))

from fractions import gcd

N, M = list(map(int,input().split()))

S = list(eval(input()))

T = list(eval(input()))

gc = gcd(N, M)

ans = N*M//gc

for i in range(gc):

    if S[N//gc*i] != T[M//gc*i]:

        ans = -1

        break

print(ans)

from fractions import gcd

N, M = list(map(int, input().split()))
S = list(eval(input()))
T = list(eval(input()))
L = gcd(N, M)
print((N * M // L if all([S[i * N // L] == T[i * M // L] for i in range(L)]) else "-1"))

from fractions import gcd

N, M = list(map(int, input().split()))
S = list(eval(input()))
T = list(eval(input()))
gc = gcd(N, M)
ans = N * M // gc
for i in range(gc):
    if S[N // gc * i] != T[M // gc * i]:
        ans = -1
        break
print(ans)

-print((N * M // L if all([S[i * N // L] == T[i * M // L] for i in range(L)]) else "-1"))


Input:
str=eval(input())

list=["a","b","c"]

if str[0] in list:

  list.remove(str[0])

  if str[1] in list:

    list.remove(str[1])

    if str[2] in list:

      print("Yes")

    else:

      print("No")

  else:

    print("No")

else:

  print("No")

string=eval(input())

list=["a","b","c"]

list.remove(string[0])

try:

  list.remove(string[1])

  try:

    list.remove(string[2])

    print("Yes")

  except:

    print("No")

except:

  print("No")

str = eval(input())
list = ["a", "b", "c"]
if str[0] in list:
    list.remove(str[0])
    if str[1] in list:
        list.remove(str[1])
        if str[2] in list:
            print("Yes")
        else:
            print("No")
    else:
        print("No")
else:
    print("No")

string = eval(input())
list = ["a", "b", "c"]
list.remove(string[0])
try:
    list.remove(string[1])
    try:
        list.remove(string[2])
        print("Yes")
    except:
        print("No")
except:
    print("No")

-str = eval(input())
+string = eval(input())
-if str[0] in list:
-    list.remove(str[0])
-    if str[1] in list:
-        list.remove(str[1])
-        if str[2] in list:
-            print("Yes")
-        else:
-            print("No")
-    else:
+list.remove(string[0])
+try:
+    list.remove(string[1])
+    try:
+        list.remove(string[2])
+        print("Yes")
+    except:
-else:
+except:


Input:
while True:

    n = int(input())

    if n == 0:

        break

    row = n

    tab = []

    for i in range(row):

        r = list(map(int, input().split()))

        r.append(sum(r))

        tab.append(r)

    bel = []

    for i in range(row):

        tmp = 0

        for j in range(row):

            tmp += tab[j][i]

        bel.append(tmp)

    bel.append(sum(bel))

    tab.append(bel)

    for i in range(row+1):

        for j in range(row+1):

            #print("".join(map("{0:>5}".format, str(tab[i][j]))))

            print('{:>5}'.format(str(tab[i][j])), end='')

        print()

def form(x, e=""):

    print("{:>5}".format(x), end=e)

while True:

    n = int(input())

    if n == 0:

        break

    rows = [list(map(int, input().split())) for _ in range(n)]

    for row in rows:

        for r in row:

            form(r)

        form(sum(row), "\n")

    columns = list(zip(*rows))

    for column in columns:

        form(sum(column))

    form(sum(sum(row) for row in rows), "\n")

while True:
    n = int(input())
    if n == 0:
        break
    row = n
    tab = []
    for i in range(row):
        r = list(map(int, input().split()))
        r.append(sum(r))
        tab.append(r)
    bel = []
    for i in range(row):
        tmp = 0
        for j in range(row):
            tmp += tab[j][i]
        bel.append(tmp)
    bel.append(sum(bel))
    tab.append(bel)
    for i in range(row + 1):
        for j in range(row + 1):
            # print("".join(map("{0:>5}".format, str(tab[i][j]))))
            print("{:>5}".format(str(tab[i][j])), end="")
        print()

-        r = list(map(int, input().split()))

+    rows = [list(map(int, input().split())) for _ in range(n)]

+    form(sum(sum(row) for row in rows), "\n")


Input:
def chg(s,n):

    res=""

    for i in s:

        o=ord(i)

        if 97<=o<=122:

            if o+n<=122:

                res+=chr(o+n)

            else:

                res+=chr(o+n-26)

        else:

            res+=i

    return res

while True:

    try:

        s=eval(input())

        for i in range(25,-1,-1):

            c=chg(s,i)

            e=c.split()

            if "the" in e or "this" in e or "that" in e:

                print(c)

                break

    except:

        break

def decode(s, n):

    a = ""

    for i in s:

        if i.islower():

            o = ord(i) + n

            if o <= 122:

                a += chr(o)

            else:

                a += chr(o - 26)

        else:

            a += i

    return a

def judge(s):

    a = s.split()

    return "the" in a or "this" in a or "that" in a

while True:

    try:

        s = eval(input())

    except:

        break

    for i in range(26):

        a = decode(s, i)

        if judge(a):

            print(a)

            break

def chg(s, n):
    res = ""
    for i in s:
        o = ord(i)
        if 97 <= o <= 122:
            if o + n <= 122:
                res += chr(o + n)
            else:
                res += chr(o + n - 26)
        else:
            res += i
    return res

while True:
    try:
        s = eval(input())
        for i in range(25, -1, -1):
            c = chg(s, i)
            e = c.split()
            if "the" in e or "this" in e or "that" in e:
                print(c)
                break
    except:
        break

+    return "the" in a or "this" in a or "that" in a

-            if "the" in e or "this" in e or "that" in e:


Input:
import sys

import itertools

# import collections

# import math

# import string

# import bisect

# import re

# import itertools

# import statistics

# bit を使ってci を管理する

def toBit(n):

    return bin(eval('0b' + str(n)))

def main():

    n = int(sys.stdin.readline().rstrip())

    schedule = [toBit(''.join(str(x) for x in sys.stdin.readline().split())) for _ in range(n)]

    profit_table = [[int(x) for x in sys.stdin.readline().split()] for _ in range(n)]

    maximum_profit = -10**10

    for open_pattern in itertools.product('01', repeat=10):

        open_pattern = toBit(''.join(open_pattern))

        if open_pattern == '0b0':

            continue

        profit = 0

        for i in range(n):

            checked = schedule[i]

            number_of_periods = bin(eval(checked) & eval(open_pattern)).count('1')

            profit += profit_table[i][number_of_periods]

        maximum_profit = max(maximum_profit, profit)

    print(maximum_profit)

if __name__ == "__main__":

    # execute only if run as a script

    main()

import sys

import itertools

# import collections

# import math

# import string

# import bisect

# import re

# import itertools

# import statistics

# bit を使ってci を管理する

def toBit(n):

    return eval('0b' + n)

def main():

    n = int(sys.stdin.readline().rstrip())

    schedule = [toBit(''.join(str(x) for x in sys.stdin.readline().split())) for _ in range(n)]

    profit_table = [[int(x) for x in sys.stdin.readline().split()] for _ in range(n)]

    maximum_profit = -10**10

    for open_pattern in itertools.product('01', repeat=10):

        open_pattern = toBit(''.join(open_pattern))

        if open_pattern == 0:

            continue

        profit = 0

        for i in range(n):

            checked = schedule[i]

            number_of_periods = bin(checked & open_pattern).count('1')

            profit += profit_table[i][number_of_periods]

        maximum_profit = max(maximum_profit, profit)

    print(maximum_profit)

if __name__ == "__main__":

    # execute only if run as a script

    main()

import sys
import itertools

# import collections
# import math
# import string
# import bisect
# import re
# import itertools
# import statistics
# bit を使ってci を管理する
def toBit(n):
    return bin(eval("0b" + str(n)))

def main():
    n = int(sys.stdin.readline().rstrip())
    schedule = [
        toBit("".join(str(x) for x in sys.stdin.readline().split())) for _ in range(n)
    ]
    profit_table = [[int(x) for x in sys.stdin.readline().split()] for _ in range(n)]
    maximum_profit = -(10**10)
    for open_pattern in itertools.product("01", repeat=10):
        open_pattern = toBit("".join(open_pattern))
        if open_pattern == "0b0":
            continue
        profit = 0
        for i in range(n):
            checked = schedule[i]
            number_of_periods = bin(eval(checked) & eval(open_pattern)).count("1")
            profit += profit_table[i][number_of_periods]
        maximum_profit = max(maximum_profit, profit)
    print(maximum_profit)

if __name__ == "__main__":
    # execute only if run as a script
    main()

import sys
import itertools

# import collections
# import math
# import string
# import bisect
# import re
# import itertools
# import statistics
# bit を使ってci を管理する
def toBit(n):
    return eval("0b" + n)

def main():
    n = int(sys.stdin.readline().rstrip())
    schedule = [
        toBit("".join(str(x) for x in sys.stdin.readline().split())) for _ in range(n)
    ]
    profit_table = [[int(x) for x in sys.stdin.readline().split()] for _ in range(n)]
    maximum_profit = -(10**10)
    for open_pattern in itertools.product("01", repeat=10):
        open_pattern = toBit("".join(open_pattern))
        if open_pattern == 0:
            continue
        profit = 0
        for i in range(n):
            checked = schedule[i]
            number_of_periods = bin(checked & open_pattern).count("1")
            profit += profit_table[i][number_of_periods]
        maximum_profit = max(maximum_profit, profit)
    print(maximum_profit)

if __name__ == "__main__":
    # execute only if run as a script
    main()

-            number_of_periods = bin(eval(checked) & eval(open_pattern)).count("1")

+            number_of_periods = bin(checked & open_pattern).count("1")


Input:
currAndRot2FBLR = {

    'N':{'N':'F','E':'R','S':'B','W':'L'},

    'E':{'E':'F','S':'R','W':'B','N':'L'},

    'S':{'S':'F','W':'R','N':'B','E':'L'},

    'W':{'W':'F','N':'R','E':'B','S':'L'}

}

dice = {

    '1': {'F':'2F', 'R':'4R', 'B':'5B', 'L':'3L'},

    '2': {'F':'6F', 'R':'4F', 'B':'1F', 'L':'3F'},

    '3': {'F':'6L', 'R':'2F', 'B':'1R', 'L':'5F'},

    '4': {'F':'6R', 'R':'5F', 'B':'1L', 'L':'2F'},

    '5': {'F':'6B', 'R':'3F', 'B':'1B', 'L':'4F'},

    '6': {'F':'5B', 'R':'4L', 'B':'2F', 'L':'3R'}

}

faces = list(map(int, input().split()))

cmd = eval(input())

## initial state

currNum = '1'

currDir = 'N'

for c in cmd:

    numDir = dice[currNum][currAndRot2FBLR[currDir][c]]

    currNum = numDir[0]

    currFBLR = numDir[1]

    currDir = {v:k for k,v in list(currAndRot2FBLR[currDir].items())}[currFBLR]

print((faces[int(currNum) - 1]))

class Dice:

    dice = {'N':2, 'E':4, 'S':5, 'W':3}

    currTop = 1

    def top(self):

        return self.currTop

    def rot(self, direction):

        newTop = self.dice[direction]

        currTop = self.currTop

        self.currTop = newTop

        if direction == 'N':

            self.dice['N'] = 7 - currTop

            self.dice['S'] = currTop

        elif direction == 'S':

            self.dice['N'] = currTop

            self.dice['S'] = 7 - currTop

        elif direction == 'E':

            self.dice['E'] = 7 - currTop

            self.dice['W'] = currTop

        elif direction == 'W':

            self.dice['E'] = currTop

            self.dice['W'] = 7 - currTop

faces = list(map(int, input().split()))

cmd = eval(input())

d = Dice()

for c in cmd:

    d.rot(c)

print((faces[d.top()-1]))

class Dice:
    dice = {"N": 2, "E": 4, "S": 5, "W": 3}
    currTop = 1

    def top(self):
        return self.currTop

    def rot(self, direction):
        newTop = self.dice[direction]
        currTop = self.currTop
        self.currTop = newTop
        if direction == "N":
            self.dice["N"] = 7 - currTop
            self.dice["S"] = currTop
        elif direction == "S":
            self.dice["N"] = currTop
            self.dice["S"] = 7 - currTop
        elif direction == "E":
            self.dice["E"] = 7 - currTop
            self.dice["W"] = currTop
        elif direction == "W":
            self.dice["E"] = currTop
            self.dice["W"] = 7 - currTop

faces = list(map(int, input().split()))
cmd = eval(input())
d = Dice()
for c in cmd:
    d.rot(c)
print((faces[d.top() - 1]))

+            self.dice["N"] = 7 - currTop

+            self.dice["S"] = 7 - currTop

+            self.dice["E"] = 7 - currTop

+            self.dice["W"] = 7 - currTop

-    numDir = dice[currNum][currAndRot2FBLR[currDir][c]]

-    currDir = {v: k for k, v in list(currAndRot2FBLR[currDir].items())}[currFBLR]


Input:
n, k = [int(i) for i in input().split()]

ans = 0

if k == 0:

    ans = n*n

else:

    for b in range(k+1, n+1):

        # number of perfect cycle

        ans += max(n // b, 0) * (b-k)

        r = n % b

        ans += max(r - k+1, 0)

print(ans)

n, k = [int(i) for i in input().split()]

ans = 0

if k == 0:

    ans = n*n

else:

    for b in range(k+1, n+1):

        # number of perfect cycle

        ans += n // b * (b-k)

        r = n % b

        ans += max(r - k+1, 0)

print(ans)

n, k = [int(i) for i in input().split()]
ans = 0
if k == 0:
    ans = n * n
else:
    for b in range(k + 1, n + 1):
        # number of perfect cycle
        ans += max(n // b, 0) * (b - k)
        r = n % b
        ans += max(r - k + 1, 0)
print(ans)

n, k = [int(i) for i in input().split()]
ans = 0
if k == 0:
    ans = n * n
else:
    for b in range(k + 1, n + 1):
        # number of perfect cycle
        ans += n // b * (b - k)
        r = n % b
        ans += max(r - k + 1, 0)
print(ans)


Input:
x,y,z = list(map(int,input().split()))

p = y + z + z

c = 0

while p <= x:

    c += 1

    p = p + y + z

print(c)

x,y,z = list(map(int,input().split()))

yz = y + z

x -= z

print((x // yz))

x, y, z = list(map(int, input().split()))
p = y + z + z
c = 0
while p <= x:
    c += 1
    p = p + y + z
print(c)

x, y, z = list(map(int, input().split()))
yz = y + z
x -= z
print((x // yz))

-p = y + z + z
-c = 0
-while p <= x:
-    c += 1
-    p = p + y + z
-print(c)
+yz = y + z
+x -= z
+print((x // yz))


Input:
s = eval(input())

s1, s2 = int(s[:2]), int(s[2:])

ans = ['NA', 'YYMM', 'MMYY', 'AMBIGUOUS']

print((ans[((1 <= s1 <= 12) << 1) + (1 <= s2 <= 12)]))


Input:
# dp

# N円を作るのに最も小さい操作回数

import sys

readline = sys.stdin.readline

N = int(readline())

dp = [i for i in range(N + 1)]

for i in range(len(dp)):

  n = 6

  while i + n < len(dp):

    if dp[i + n] > dp[i] + 1:

      dp[i + n] = dp[i] + 1

    n *= 6

  n = 9

  while i + n < len(dp):

    if dp[i + n] > dp[i] + 1:

      dp[i + n] = dp[i] + 1

    n *= 9

print((dp[N]))

import sys

readline = sys.stdin.readline

N = int(readline())

INF = 10 ** 10

dp = [INF] * (N + 1)

dp[0] = 0

for i in range(10,0,-1):

  for j in range(N + 1):

    if dp[j] != INF:

      if j + 6 ** i <= N:

        dp[j + 6 ** i] = min(dp[j + 6 ** i], dp[j] + 1)

      if j + 9 ** i <= N:

        dp[j + 9 ** i] = min(dp[j + 9 ** i], dp[j] + 1)

ans = INF

for i in range(len(dp) - 1, -1, -1):

  money = dp[i] + N - i

  if ans > money:

    ans = money

print(ans)

import sys

readline = sys.stdin.readline
N = int(readline())
INF = 10**10
dp = [INF] * (N + 1)
dp[0] = 0
for i in range(10, 0, -1):
    for j in range(N + 1):
        if dp[j] != INF:
            if j + 6**i <= N:
                dp[j + 6**i] = min(dp[j + 6**i], dp[j] + 1)
            if j + 9**i <= N:
                dp[j + 9**i] = min(dp[j + 9**i], dp[j] + 1)
ans = INF
for i in range(len(dp) - 1, -1, -1):
    money = dp[i] + N - i
    if ans > money:
        ans = money
print(ans)

-# N円を作るのに最も小さい操作回数


Input:
x, n = list(map(int,input().split()))

L = list(map(int,input().split()))

l, r = x-1, x+1

if n == 0 or x not in L:

  print(x)

  exit()

while True:

  flg = True

  if l not in L:

    print(l)

    break

  elif r not in L:

    print(r)

    break

  l -= 1

  r += 1

x, n = list(map(int,input().split()))

L = list(map(int,input().split()))

l, r = x-1, x+1

if n == 0 or x not in L:

  print(x)

  exit()

while True:

  if l not in L:

    print(l)

    break

  elif r not in L:

    print(r)

    break

  l -= 1

  r += 1

x, n = list(map(int, input().split()))
L = list(map(int, input().split()))
l, r = x - 1, x + 1
if n == 0 or x not in L:
    print(x)
    exit()
while True:
    flg = True
    if l not in L:
        print(l)
        break
    elif r not in L:
        print(r)
        break
    l -= 1
    r += 1

x, n = list(map(int, input().split()))
L = list(map(int, input().split()))
l, r = x - 1, x + 1
if n == 0 or x not in L:
    print(x)
    exit()
while True:
    if l not in L:
        print(l)
        break
    elif r not in L:
        print(r)
        break
    l -= 1
    r += 1


Input:
import numpy as np

def divisor(n):

    i = 1

    table = []

    while i * i <= n:

        if n%i == 0:

            table.append(i)

            table.append(n//i)

        i += 1

    table = list(set(table))

    table = sorted(table)

    return table

def make_prime(U):

    is_prime = np.zeros(U,np.bool)

    is_prime[2] = 1

    is_prime[3::2] = 1

    M = int(U**.5)+1

    for p in range(3,M,2):

        if is_prime[p]:

            is_prime[p*p::p+p] = 0

    return is_prime, is_prime.nonzero()[0]

A, B = list(map(int, input().split()))

def is_prime(n):

    if n == 1:

        return False

    for i in range(2,int(n**0.5)+1):

        if n % i == 0:

            return False

    return True

A_list = set(divisor(A))

B_list = set(divisor(B))

list = A_list & B_list

#_, primes = make_prime(10**8)

cnt = 1

for i in list:

    if is_prime(i):

        cnt += 1

print(cnt)

def divisor(n):

    i = 1

    table = []

    while i * i <= n:

        if n%i == 0:

            table.append(i)

            table.append(n//i)

        i += 1

    table = list(set(table))

    table = sorted(table)

    return table

def is_prime(n):

    if n == 1:

        return False

    for i in range(2,int(n**0.5)+1):

        if n % i == 0:

            return False

    return True

A, B = list(map(int, input().split()))

A_list = set(divisor(A))

B_list = set(divisor(B))

list = A_list & B_list

cnt = 1

for i in list:

    if is_prime(i):

        cnt += 1

print(cnt)

import numpy as np

def divisor(n):
    i = 1
    table = []
    while i * i <= n:
        if n % i == 0:
            table.append(i)
            table.append(n // i)
        i += 1
    table = list(set(table))
    table = sorted(table)
    return table

def make_prime(U):
    is_prime = np.zeros(U, np.bool)
    is_prime[2] = 1
    is_prime[3::2] = 1
    M = int(U**0.5) + 1
    for p in range(3, M, 2):
        if is_prime[p]:
            is_prime[p * p :: p + p] = 0
    return is_prime, is_prime.nonzero()[0]

A, B = list(map(int, input().split()))

def is_prime(n):
    if n == 1:
        return False
    for i in range(2, int(n**0.5) + 1):
        if n % i == 0:
            return False
    return True

A_list = set(divisor(A))
B_list = set(divisor(B))
list = A_list & B_list
# _, primes = make_prime(10**8)
cnt = 1
for i in list:
    if is_prime(i):
        cnt += 1
print(cnt)

def divisor(n):
    i = 1
    table = []
    while i * i <= n:
        if n % i == 0:
            table.append(i)
            table.append(n // i)
        i += 1
    table = list(set(table))
    table = sorted(table)
    return table

def is_prime(n):
    if n == 1:
        return False
    for i in range(2, int(n**0.5) + 1):
        if n % i == 0:
            return False
    return True

A, B = list(map(int, input().split()))
A_list = set(divisor(A))
B_list = set(divisor(B))
list = A_list & B_list
cnt = 1
for i in list:
    if is_prime(i):
        cnt += 1
print(cnt)

-            is_prime[p * p :: p + p] = 0

-    return is_prime, is_prime.nonzero()[0]


Input:
N,M=list(map(int,input().split()))

k=[0]*M

s=[[] for _ in range(M)]

for i in range(M):

    a=list(map(int,input().split()))

    k[i]=a[0]

    s[i]=a[1:]

    s[i].sort()

p=list(map(int,input().split()))

ans=0

for i in range(pow(2,N)):

    light=0

    for j in range(M):

        temp=0

        for kk in range(k[j]):

            a=i>>(s[j][kk]-1)

            if a&1==1:

                temp+=1

        if temp%2!=p[j]:

            break

        light+=1

    if light==M:

        ans+=1

print(ans)

import sys

input = sys.stdin.readline

def I(): return int(eval(input()))

def MI(): return list(map(int, input().split()))

def LI(): return list(map(int, input().split()))

def main():

    mod=10**9+7

    N,M=MI()

    s=[]

    for i in range(M):

        L=LI()

        s.append(L[1:])

    p=LI()

    import itertools

    ans=0

    for ite in itertools.product([0,1], repeat=N):

        flag=1

        for i in range(M):

            temp=0

            for ss in s[i]:

                if ite[ss-1]:

                    temp+=1

            if temp%2==p[i]:

                pass

            else:

                flag=0

        if flag:

            ans+=1

    print(ans)

main()

N, M = list(map(int, input().split()))
k = [0] * M
s = [[] for _ in range(M)]
for i in range(M):
    a = list(map(int, input().split()))
    k[i] = a[0]
    s[i] = a[1:]
    s[i].sort()
p = list(map(int, input().split()))
ans = 0
for i in range(pow(2, N)):
    light = 0
    for j in range(M):
        temp = 0
        for kk in range(k[j]):
            a = i >> (s[j][kk] - 1)
            if a & 1 == 1:
                temp += 1
        if temp % 2 != p[j]:
            break
        light += 1
    if light == M:
        ans += 1
print(ans)

import sys

input = sys.stdin.readline

def I():
    return int(eval(input()))

def MI():
    return list(map(int, input().split()))

def LI():
    return list(map(int, input().split()))

def main():
    mod = 10**9 + 7
    N, M = MI()
    s = []
    for i in range(M):
        L = LI()
        s.append(L[1:])
    p = LI()
    import itertools

    ans = 0
    for ite in itertools.product([0, 1], repeat=N):
        flag = 1
        for i in range(M):
            temp = 0
            for ss in s[i]:
                if ite[ss - 1]:
                    temp += 1
            if temp % 2 == p[i]:
                pass
            else:
                flag = 0
        if flag:
            ans += 1
    print(ans)

main()

-    a = list(map(int, input().split()))

+    return list(map(int, input().split()))

+    for ite in itertools.product([0, 1], repeat=N):


Input:
import itertools

from scipy.sparse.csgraph import floyd_warshall

N,M,R=list(map(int,input().split()))

r=list(map(int,input().split()))

l=[list(map(int,input().split())) for i in range(M)]

s=[[float('inf') for i in range(N)] for i in range(N)]

for i in range(M):

    a,b,c=l[i]

    s[a-1][b-1]=c

    s[b-1][a-1]=c

for i in range(N):

    s[i][i]=0

s=floyd_warshall(s)

#print(s)

ans=10**9

for i in itertools.permutations(r):

#    print(i)

    temp_ans=0

    for j in range(R-1):

        temp_ans+=s[i[j]-1][i[j+1]-1]

    ans=min(ans,temp_ans)

print((int(ans)))

import itertools

from scipy.sparse.csgraph import floyd_warshall

n,m,r=list(map(int,input().split()))

r_l=list(map(int,input().split()))

l=[list(map(int,input().split())) for i in range(m)]

d=[[float('inf')]*n for i in range(n)]

for i in range(n):

    d[i][i]=0

for i in l:

    d[i[0]-1][i[1]-1]=i[2]

    d[i[1]-1][i[0]-1]=i[2]

d=floyd_warshall(d)

ans=10**9

for i in itertools.permutations(r_l):

    t_ans=0

    for j in range(len(i)-1):

        t_ans+=d[i[j]-1][i[j+1]-1]

    ans=min(ans,t_ans)

print((int(ans)))

import itertools
from scipy.sparse.csgraph import floyd_warshall

N, M, R = list(map(int, input().split()))
r = list(map(int, input().split()))
l = [list(map(int, input().split())) for i in range(M)]
s = [[float("inf") for i in range(N)] for i in range(N)]
for i in range(M):
    a, b, c = l[i]
    s[a - 1][b - 1] = c
    s[b - 1][a - 1] = c
for i in range(N):
    s[i][i] = 0
s = floyd_warshall(s)
# print(s)
ans = 10**9
for i in itertools.permutations(r):
    #    print(i)
    temp_ans = 0
    for j in range(R - 1):
        temp_ans += s[i[j] - 1][i[j + 1] - 1]
    ans = min(ans, temp_ans)
print((int(ans)))

import itertools
from scipy.sparse.csgraph import floyd_warshall

n, m, r = list(map(int, input().split()))
r_l = list(map(int, input().split()))
l = [list(map(int, input().split())) for i in range(m)]
d = [[float("inf")] * n for i in range(n)]
for i in range(n):
    d[i][i] = 0
for i in l:
    d[i[0] - 1][i[1] - 1] = i[2]
    d[i[1] - 1][i[0] - 1] = i[2]
d = floyd_warshall(d)
ans = 10**9
for i in itertools.permutations(r_l):
    t_ans = 0
    for j in range(len(i) - 1):
        t_ans += d[i[j] - 1][i[j + 1] - 1]
    ans = min(ans, t_ans)
print((int(ans)))

-N, M, R = list(map(int, input().split()))

-l = [list(map(int, input().split())) for i in range(M)]

-s = [[float("inf") for i in range(N)] for i in range(N)]

+n, m, r = list(map(int, input().split()))

+l = [list(map(int, input().split())) for i in range(m)]

+d = [[float("inf")] * n for i in range(n)]


Input:
from collections import defaultdict

N, T = list(map(int, input().split()))

A = [int(x) for x in input().split()]

B = [0] * N

for i in range(N - 1):

    a = A[N - i - 1]

    B[i + 1] = max(B[i], a)

B = B[::-1]

c = defaultdict(int)

for i in range(N - 1):

    c[B[i] - A[i]] += 1

print((c[max(c)]))

from itertools import accumulate

from collections import defaultdict

N, T = list(map(int, input().split()))

A = [int(x) for x in input().split()]

B = [x for x in accumulate(A[::-1], max)][::-1]

c = defaultdict(int)

for i in range(N - 1):

    c[B[i] - A[i]] += 1

print((c[max(c)]))

+B = [x for x in accumulate(A[::-1], max)][::-1]


Input:
s = eval(input())

t = input()[:-1]

if s == t:

  print("Yes")

else:

  print("No")

if eval(input()) == input()[:-1]:print("Yes")

else: print("No")

if eval(input()) == input()[:-1]:
    print("Yes")
else:
    print("No")

-s = eval(input())
-t = input()[:-1]
-if s == t:
+if eval(input()) == input()[:-1]:


Input:
# import sys

# input = sys.stdin.readline

n, k = list(map(int, input().split()))

h = [0]*n

for i in range(n):

    h[i] = int(eval(input()))

h.sort()

ans = 10**9

for i in range(n-k+1):

    temp = h[i+k-1] - h[i]

    if ans > temp:

        ans = temp

print(ans)

import sys

input = sys.stdin.readline

n, k = list(map(int, input().split()))

# h = [0]*n

# for i in range(n):

#     h[i] = int(input())

h = [int(eval(input())) for _ in range(n)]

h.sort()

ans = 10**9

for i in range(n-k+1):

    temp = h[i+k-1] - h[i]

    ans = min(ans, temp)

print(ans)

# import sys
# input = sys.stdin.readline
n, k = list(map(int, input().split()))
h = [0] * n
for i in range(n):
    h[i] = int(eval(input()))
h.sort()
ans = 10**9
for i in range(n - k + 1):
    temp = h[i + k - 1] - h[i]
    if ans > temp:
        ans = temp
print(ans)

import sys

input = sys.stdin.readline
n, k = list(map(int, input().split()))
# h = [0]*n
# for i in range(n):
#     h[i] = int(input())
h = [int(eval(input())) for _ in range(n)]
h.sort()
ans = 10**9
for i in range(n - k + 1):
    temp = h[i + k - 1] - h[i]
    ans = min(ans, temp)
print(ans)

+h = [int(eval(input())) for _ in range(n)]


Input:
import sys

sys.setrecursionlimit(10**9)

N = int(eval(input()))

adjL = [[] for _ in range(N)]

for _ in range(N-1):

    u, v, w = list(map(int, input().split()))

    adjL[u-1].append((v-1, w))

    adjL[v-1].append((u-1, w))

def dfs(vNow):

    useds[vNow] = True

    ans = anss[vNow]

    for v2, wt in adjL[vNow]:

        if useds[v2]: continue

        if wt % 2:  # 長さが奇数ならば、色を反転

            anss[v2] = 1-ans

        else:

            anss[v2] = ans

        dfs(v2)

useds = [False] * N

anss = [0] * N

anss[0] = 0

dfs(0)

print(('\n'.join(map(str, anss))))

import sys

input = sys.stdin.readline

sys.setrecursionlimit(10**9)

N = int(eval(input()))

adjL = [[] for _ in range(N)]

for _ in range(N-1):

    u, v, w = list(map(int, input().split()))

    u, v = u-1, v-1

    adjL[u].append((v, w))

    adjL[v].append((u, w))

colors = [0] * N

def dfs(vNow, vPar):

    color = colors[vNow]

    for v2, wt in adjL[vNow]:

        if v2 == vPar: continue

        if wt%2:

            colors[v2] = 1-color

        else:

            colors[v2] = color

        dfs(v2, vNow)

dfs(0, -1)

print(('\n'.join(map(str, colors))))

import sys

sys.setrecursionlimit(10**9)
N = int(eval(input()))
adjL = [[] for _ in range(N)]
for _ in range(N - 1):
    u, v, w = list(map(int, input().split()))
    adjL[u - 1].append((v - 1, w))
    adjL[v - 1].append((u - 1, w))

def dfs(vNow):
    useds[vNow] = True
    ans = anss[vNow]
    for v2, wt in adjL[vNow]:
        if useds[v2]:
            continue
        if wt % 2:  # 長さが奇数ならば、色を反転
            anss[v2] = 1 - ans
        else:
            anss[v2] = ans
        dfs(v2)

useds = [False] * N
anss = [0] * N
anss[0] = 0
dfs(0)
print(("\n".join(map(str, anss))))

import sys

input = sys.stdin.readline
sys.setrecursionlimit(10**9)
N = int(eval(input()))
adjL = [[] for _ in range(N)]
for _ in range(N - 1):
    u, v, w = list(map(int, input().split()))
    u, v = u - 1, v - 1
    adjL[u].append((v, w))
    adjL[v].append((u, w))
colors = [0] * N

def dfs(vNow, vPar):
    color = colors[vNow]
    for v2, wt in adjL[vNow]:
        if v2 == vPar:
            continue
        if wt % 2:
            colors[v2] = 1 - color
        else:
            colors[v2] = color
        dfs(v2, vNow)

dfs(0, -1)
print(("\n".join(map(str, colors))))

+input = sys.stdin.readline
-    adjL[u - 1].append((v - 1, w))
-    adjL[v - 1].append((u - 1, w))
+    u, v = u - 1, v - 1
+    adjL[u].append((v, w))
+    adjL[v].append((u, w))
+colors = [0] * N
-def dfs(vNow):
-    useds[vNow] = True
-    ans = anss[vNow]
+def dfs(vNow, vPar):
+    color = colors[vNow]
-        if useds[v2]:
+        if v2 == vPar:
-        if wt % 2:  # 長さが奇数ならば、色を反転
-            anss[v2] = 1 - ans
+        if wt % 2:
+            colors[v2] = 1 - color
-            anss[v2] = ans
-        dfs(v2)
+            colors[v2] = color
+        dfs(v2, vNow)
-useds = [False] * N
-anss = [0] * N
-anss[0] = 0
-dfs(0)
-print(("\n".join(map(str, anss))))
+dfs(0, -1)
+print(("\n".join(map(str, colors))))


Input:
N=int(eval(input()))

A=[((i+1),int(eval(input()))) for i in range(N)]

count = 0

b=1

for i in range(N+1):

  b = A[b-1][1]

  count+= 1

  if b==2:

    print(count)

    break

if count == N+1:

  print((-1))

N=int(eval(input()))

A=[(int(eval(input()))) for i in range(N)]

count = 0

b=1

for i in range(N+1):

  b = A[b-1]

  count+= 1

  if b==2:

    print(count)

    break

if count == N+1:

  print((-1))

N = int(eval(input()))
A = [((i + 1), int(eval(input()))) for i in range(N)]
count = 0
b = 1
for i in range(N + 1):
    b = A[b - 1][1]
    count += 1
    if b == 2:
        print(count)
        break
if count == N + 1:
    print((-1))

N = int(eval(input()))
A = [(int(eval(input()))) for i in range(N)]
count = 0
b = 1
for i in range(N + 1):
    b = A[b - 1]
    count += 1
    if b == 2:
        print(count)
        break
if count == N + 1:
    print((-1))

-A = [((i + 1), int(eval(input()))) for i in range(N)]

+A = [(int(eval(input()))) for i in range(N)]


Input: print(input() ** 3)

-print(input() ** 3)
+x = eval(input())
+print(x * x * x)


Input:
import sys

from collections import deque

input = sys.stdin.readline

N = int(eval(input()))

X = [deque() for _ in range(N)]

P = [-1] * N

for i in range(N-1):

    a, b = list(map(int, input().split()))

    X[a-1].append((i, b-1))

    #print(X)

    #P[b-1] = a-1

#print(X)

Q = deque([(0, -1)])

ANS = [-1] * (N-1)

cmax = 0

while Q:

    x, c0 = Q.pop()

    c = 1

    for i, y in X[x]:

        if c == c0: c += 1

        ANS[i] = c

        Q.appendleft((y, c))

        c += 1

    cmax = max(cmax, c-1)

print(cmax)

for ans in ANS:

    print(ans)

import sys

from collections import deque

input = sys.stdin.readline

N = int(eval(input()))

X = [[] for _ in range(N)]

P = [-1] * N

for i in range(N-1):

    a, b = list(map(int, input().split()))

    X[a-1].append((i, b-1))

    #print(X)

    #P[b-1] = a-1

#print(X)

Q = deque([(0, -1)])

ANS = [-1] * (N-1)

cmax = 0

while Q:

    x, c0 = Q.pop()

    c = 1

    for i, y in X[x]:

        if c == c0: c += 1

        ANS[i] = c

        Q.appendleft((y, c))

        c += 1

    cmax = max(cmax, c-1)

print(cmax)

for ans in ANS:

    print(ans)

import sys
from collections import deque

input = sys.stdin.readline
N = int(eval(input()))
X = [deque() for _ in range(N)]
P = [-1] * N
for i in range(N - 1):
    a, b = list(map(int, input().split()))
    X[a - 1].append((i, b - 1))
    # print(X)
    # P[b-1] = a-1
# print(X)
Q = deque([(0, -1)])
ANS = [-1] * (N - 1)
cmax = 0
while Q:
    x, c0 = Q.pop()
    c = 1
    for i, y in X[x]:
        if c == c0:
            c += 1
        ANS[i] = c
        Q.appendleft((y, c))
        c += 1
    cmax = max(cmax, c - 1)
print(cmax)
for ans in ANS:
    print(ans)

import sys
from collections import deque

input = sys.stdin.readline
N = int(eval(input()))
X = [[] for _ in range(N)]
P = [-1] * N
for i in range(N - 1):
    a, b = list(map(int, input().split()))
    X[a - 1].append((i, b - 1))
    # print(X)
    # P[b-1] = a-1
# print(X)
Q = deque([(0, -1)])
ANS = [-1] * (N - 1)
cmax = 0
while Q:
    x, c0 = Q.pop()
    c = 1
    for i, y in X[x]:
        if c == c0:
            c += 1
        ANS[i] = c
        Q.appendleft((y, c))
        c += 1
    cmax = max(cmax, c - 1)
print(cmax)
for ans in ANS:
    print(ans)

-X = [deque() for _ in range(N)]
+X = [[] for _ in range(N)]


Input:
import copy

def actual(n, P):

    nums_asc = sorted(P)

    if nums_asc == P:

        return 'YES'

    N = len(P)

    for i in range(N):

        for j in range(1, N):

            swapped_nums = copy.deepcopy(P)

            swapped_nums[i] = P[j]

            swapped_nums[j] = P[i]

            if swapped_nums == nums_asc:

                return 'YES'

    return 'NO'

n = int(eval(input()))

P = list(map(int, input().split()))

print((actual(n, P)))

def actual(n, P):

    # 正しくない配置にある数字が2つ以下なら、1回以下のSwapで照準になる

    n_unmatched = [i == p for i, p in enumerate(P, start=1)].count(False)

    if n_unmatched <= 2:

        return 'YES'

    return 'NO'

n = int(eval(input()))

P = list(map(int, input().split()))

print((actual(n, P)))

import copy

def actual(n, P):
    nums_asc = sorted(P)
    if nums_asc == P:
        return "YES"
    N = len(P)
    for i in range(N):
        for j in range(1, N):
            swapped_nums = copy.deepcopy(P)
            swapped_nums[i] = P[j]
            swapped_nums[j] = P[i]
            if swapped_nums == nums_asc:
                return "YES"
    return "NO"

n = int(eval(input()))
P = list(map(int, input().split()))
print((actual(n, P)))

def actual(n, P):
    # 正しくない配置にある数字が2つ以下なら、1回以下のSwapで照準になる
    n_unmatched = [i == p for i, p in enumerate(P, start=1)].count(False)
    if n_unmatched <= 2:
        return "YES"
    return "NO"

n = int(eval(input()))
P = list(map(int, input().split()))
print((actual(n, P)))

+    # 正しくない配置にある数字が2つ以下なら、1回以下のSwapで照準になる

+    n_unmatched = [i == p for i, p in enumerate(P, start=1)].count(False)

-            swapped_nums = copy.deepcopy(P)

-            if swapped_nums == nums_asc:


Input:
# -*- coding: utf-8 -*-

from math import log2

line = list(map(int, input().split(" ")))

n = line[0]

m = line[1]

d = line[2]

if 2*d<n and d>0:

    ret1 = 1+(m-2)*log2(n)

    ret2 = log2(n-d)

    ret3 = log2(m-1)

    ret4 = m*log2(n)

    print((2**(ret1+ret2+ret3-ret4)))

else:

    ret1 = (m-1)*log2(n)

    ret2 = log2(m-1)

    ret3 = m*log2(n)

    print((2**(ret1+ret2-ret3)))

n,m,d = list(map(int, input().split()))

if d == 0:

    print(((m-1)/n))

else:

    print((2*(n-d)*(m-1)/n/n))

# -*- coding: utf-8 -*-
from math import log2

line = list(map(int, input().split(" ")))
n = line[0]
m = line[1]
d = line[2]
if 2 * d < n and d > 0:
    ret1 = 1 + (m - 2) * log2(n)
    ret2 = log2(n - d)
    ret3 = log2(m - 1)
    ret4 = m * log2(n)
    print((2 ** (ret1 + ret2 + ret3 - ret4)))
else:
    ret1 = (m - 1) * log2(n)
    ret2 = log2(m - 1)
    ret3 = m * log2(n)
    print((2 ** (ret1 + ret2 - ret3)))

n, m, d = list(map(int, input().split()))
if d == 0:
    print(((m - 1) / n))
else:
    print((2 * (n - d) * (m - 1) / n / n))

-line = list(map(int, input().split(" ")))

-    print((2 ** (ret1 + ret2 + ret3 - ret4)))

+n, m, d = list(map(int, input().split()))


Input:
N, M = list(map(int, input().split()))

sc = [list(map(int, input().split())) for _ in range(M)]

L = [-1] * N

for s, c in sc:

  s -= 1

  if L[s] == -1:

    L[s] = c

  else:

    if L[s] != c:

      print((-1))

      exit()

if N == 1:

  if L[0] == -1:

    print((0))

  else:

    print((L[0]))

else:

  if L[0] == 0:

    print((-1))

    exit()

  if L[0] == -1:

    ans = 1

  else:

    ans = L[0]

  for i in range(1, N):

    ans *= 10

    if L[i] != -1:

      ans += L[i]

  print(ans)

N, M = list(map(int, input().split()))

sc = [list(map(int, input().split())) for _ in range(M)]

t = 10 ** (N - 1)

e = t * 10

if N == 1:

  t = 0

for i in range(t, e):

  k = list(str(i))

  for s, c in sc:

    if int(k[s - 1]) != c:

      break

  else:

    print(i)

    exit()

print((-1))


Input:
x, y, z = list(map(int, input().split()))

if y - 1 >= z:

	print(((x * z) // y - x * (z // y)))

else:

	w = (z // y) * y - 1

	print((max((x * w) // y - x * (w // y), (x * z) // y - x * (z // y))))

a,b,n=list(map(int,input().split()))

f=lambda x:(a*x)//b-a*(x//b)

print((f(min(b-1,n))))

a, b, n = list(map(int, input().split()))
f = lambda x: (a * x) // b - a * (x // b)
print((f(min(b - 1, n))))

-x, y, z = list(map(int, input().split()))

+a, b, n = list(map(int, input().split()))

+f = lambda x: (a * x) // b - a * (x // b)


Input:
from collections import defaultdict, deque, Counter

from heapq import heappush, heappop, heapify

import math

import bisect

import random

from itertools import permutations, accumulate, combinations, product

import sys

import string

from bisect import bisect_left, bisect_right

from math import factorial, ceil, floor

from functools import reduce

sys.setrecursionlimit(2147483647)

INF = 10 ** 15

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

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

def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()

def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')

def IR(n): return [I() for i in range(n)]

def LIR(n): return [LI() for i in range(n)]

def SR(n): return [S() for i in range(n)]

def LSR(n): return [LS() for i in range(n)]

def SRL(n): return [list(S()) for i in range(n)]

def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]

mod = 1000000007

n, m = LI()

s, t = LI()

G = [[] for _ in range(n)]

for i in range(m):

    a, b, c = LI()

    G[a - 1] += [(b - 1, c)]

    G[b - 1] += [(a - 1, c)]

def dijkstra(graph, start=0):

    route_cnt = [start] * n

    route_cnt[start] = 1

    que = [(0, start)]

    dist = [INF] * n

    dist[start] = 0

    while que:

        min_dist, u = heappop(que)

        if min_dist > dist[u]:

            continue

        for v, c in graph[u]:

            if dist[u] + c < dist[v]:

                dist[v] = dist[u] + c

                route_cnt[v] = route_cnt[u]

                heappush(que, (dist[u] + c  , v))

            elif dist[u] + c == dist[v]:

                route_cnt[v] = (route_cnt[v] + route_cnt[u]) % mod

    return route_cnt, dist

route_cnt, dist = dijkstra(G, s - 1)

r_route_cnt, r_dist = dijkstra(G, t - 1)

total_dist = dist[t - 1]

ret = 0

for i in range(n):

    if dist[i] == r_dist[i]:

        ret = ret + (route_cnt[i] * r_route_cnt[i] % mod) ** 2 % mod

for u in range(n):

    for v, c in G[u]:

        if dist[u] < total_dist / 2 < dist[v] and dist[u] + c + r_dist[v] == total_dist:

            ret = (ret + route_cnt[u] ** 2 % mod * r_route_cnt[v] ** 2 % mod) % mod

print(((route_cnt[t - 1] ** 2 - ret) % mod))

from collections import defaultdict, deque, Counter

from heapq import heappush, heappop, heapify

import math

from bisect import bisect_left, bisect_right

import random

from itertools import permutations, accumulate, combinations

import sys

import string

from copy import deepcopy

INF = 10 ** 20

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

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

def LS(): return sys.stdin.readline().split()

def S(): return sys.stdin.readline().strip()

def IR(n): return [I() for i in range(n)]

def LIR(n): return [LI() for i in range(n)]

def SR(n): return [S() for i in range(n)]

def LSR(n): return [LS() for i in range(n)]

def SRL(n): return [list(S()) for i in range(n)]

def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]

mod = 10 ** 9 + 7

def dijkstra(s):

    hq=[(0,s)]

    dist=[INF]*n

    dp=[0]*n

    dp[s]=1

    dist[s]=0

    checked=[0]*n

    while hq:

        min_dist,u=heappop(hq)

        if checked[u]:

            continue

        checked[u]=1

        for v,c in G[u]:

            if dist[u]+c<dist[v]:

                dist[v]=dist[u]+c

                dp[v]=dp[u]

                heappush(hq,(dist[v],v))

            elif dist[u]+c==dist[v]:

                dp[v]+=dp[u]

                dp[v]%=mod

    return dp, dist

n, m = LI()

s, t = LI()

G = [[] for _ in range(n)]

for _ in range(m):

    a,b, d = LI()

    G[a - 1] += [(b - 1, d)]

    G[b - 1] += [(a - 1, d)]

dp1,dist1=dijkstra(s-1)

dp2,dist2=dijkstra(t-1)

total=dist1[t-1]

ans=dp1[t-1]*dp2[s-1]%mod

for i in range(n):

    if dist1[i]==dist2[i]==total/2:

        ans-=pow(dp1[i]*dp2[i]%mod,2,mod)

for j in range(n):

    for k, ci in G[j]:

        if dist1[j]+dist2[k]+ci==total and dist1[j]<total/2 and dist2[k]<total/2:

            ans-=pow(dp1[j]*dp2[k]%mod,2,mod)

print((ans%mod))

from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from functools import reduce

sys.setrecursionlimit(2147483647)
INF = 10**15

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

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

def LS():
    return sys.stdin.buffer.readline().rstrip().decode("utf-8").split()

def S():
    return sys.stdin.buffer.readline().rstrip().decode("utf-8")

def IR(n):
    return [I() for i in range(n)]

def LIR(n):
    return [LI() for i in range(n)]

def SR(n):
    return [S() for i in range(n)]

def LSR(n):
    return [LS() for i in range(n)]

def SRL(n):
    return [list(S()) for i in range(n)]

def MSRL(n):
    return [[int(j) for j in list(S())] for i in range(n)]

mod = 1000000007
n, m = LI()
s, t = LI()
G = [[] for _ in range(n)]
for i in range(m):
    a, b, c = LI()
    G[a - 1] += [(b - 1, c)]
    G[b - 1] += [(a - 1, c)]

def dijkstra(graph, start=0):
    route_cnt = [start] * n
    route_cnt[start] = 1
    que = [(0, start)]
    dist = [INF] * n
    dist[start] = 0
    while que:
        min_dist, u = heappop(que)
        if min_dist > dist[u]:
            continue
        for v, c in graph[u]:
            if dist[u] + c < dist[v]:
                dist[v] = dist[u] + c
                route_cnt[v] = route_cnt[u]
                heappush(que, (dist[u] + c, v))
            elif dist[u] + c == dist[v]:
                route_cnt[v] = (route_cnt[v] + route_cnt[u]) % mod
    return route_cnt, dist

route_cnt, dist = dijkstra(G, s - 1)
r_route_cnt, r_dist = dijkstra(G, t - 1)
total_dist = dist[t - 1]
ret = 0
for i in range(n):
    if dist[i] == r_dist[i]:
        ret = ret + (route_cnt[i] * r_route_cnt[i] % mod) ** 2 % mod
for u in range(n):
    for v, c in G[u]:
        if dist[u] < total_dist / 2 < dist[v] and dist[u] + c + r_dist[v] == total_dist:
            ret = (ret + route_cnt[u] ** 2 % mod * r_route_cnt[v] ** 2 % mod) % mod
print(((route_cnt[t - 1] ** 2 - ret) % mod))

from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
from bisect import bisect_left, bisect_right
import random
from itertools import permutations, accumulate, combinations
import sys
import string
from copy import deepcopy

INF = 10**20

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

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

def LS():
    return sys.stdin.readline().split()

def S():
    return sys.stdin.readline().strip()

def IR(n):
    return [I() for i in range(n)]

def LIR(n):
    return [LI() for i in range(n)]

def SR(n):
    return [S() for i in range(n)]

def LSR(n):
    return [LS() for i in range(n)]

def SRL(n):
    return [list(S()) for i in range(n)]

def MSRL(n):
    return [[int(j) for j in list(S())] for i in range(n)]

mod = 10**9 + 7

def dijkstra(s):
    hq = [(0, s)]
    dist = [INF] * n
    dp = [0] * n
    dp[s] = 1
    dist[s] = 0
    checked = [0] * n
    while hq:
        min_dist, u = heappop(hq)
        if checked[u]:
            continue
        checked[u] = 1
        for v, c in G[u]:
            if dist[u] + c < dist[v]:
                dist[v] = dist[u] + c
                dp[v] = dp[u]
                heappush(hq, (dist[v], v))
            elif dist[u] + c == dist[v]:
                dp[v] += dp[u]
                dp[v] %= mod
    return dp, dist

n, m = LI()
s, t = LI()
G = [[] for _ in range(n)]
for _ in range(m):
    a, b, d = LI()
    G[a - 1] += [(b - 1, d)]
    G[b - 1] += [(a - 1, d)]
dp1, dist1 = dijkstra(s - 1)
dp2, dist2 = dijkstra(t - 1)
total = dist1[t - 1]
ans = dp1[t - 1] * dp2[s - 1] % mod
for i in range(n):
    if dist1[i] == dist2[i] == total / 2:
        ans -= pow(dp1[i] * dp2[i] % mod, 2, mod)
for j in range(n):
    for k, ci in G[j]:
        if (
            dist1[j] + dist2[k] + ci == total
            and dist1[j] < total / 2
            and dist2[k] < total / 2
        ):
            ans -= pow(dp1[j] * dp2[k] % mod, 2, mod)
print((ans % mod))

+from bisect import bisect_left, bisect_right

-from itertools import permutations, accumulate, combinations, product

+from itertools import permutations, accumulate, combinations

-from bisect import bisect_left, bisect_right

-from math import factorial, ceil, floor

-    return list(map(int, sys.stdin.buffer.readline().split()))

+    return list(map(int, sys.stdin.readline().split()))

-    return int(sys.stdin.buffer.readline())

-    return sys.stdin.buffer.readline().rstrip().decode("utf-8").split()

+    return sys.stdin.readline().split()

+    return sys.stdin.readline().strip()

+                heappush(hq, (dist[v], v))

+            elif dist[u] + c == dist[v]:

-                route_cnt[v] = route_cnt[u]

-                heappush(que, (dist[u] + c, v))

-            elif dist[u] + c == dist[v]:

-                route_cnt[v] = (route_cnt[v] + route_cnt[u]) % mod

-r_route_cnt, r_dist = dijkstra(G, t - 1)

-        ret = ret + (route_cnt[i] * r_route_cnt[i] % mod) ** 2 % mod

-        if dist[u] < total_dist / 2 < dist[v] and dist[u] + c + r_dist[v] == total_dist:

-            ret = (ret + route_cnt[u] ** 2 % mod * r_route_cnt[v] ** 2 % mod) % mod

-print(((route_cnt[t - 1] ** 2 - ret) % mod))

+    if dist1[i] == dist2[i] == total / 2:

+        ans -= pow(dp1[i] * dp2[i] % mod, 2, mod)

+            dist1[j] + dist2[k] + ci == total

+            ans -= pow(dp1[j] * dp2[k] % mod, 2, mod)


Input:
from collections import deque

N = int(eval(input()))

A = deque()

for _ in range(N):

    a,b = list(map(int,input().split()))

    A.append([b,a])

A = list(A)

A.sort()

time = 0

for i in A:

    time += i[1]

    if time > i[0]:

        print('No')

        exit()

print('Yes')

import sys

import math

import itertools

import bisect

from copy import copy

from collections import deque,Counter

from decimal import Decimal

def s(): return eval(input())

def i(): return int(eval(input()))

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

def I(): return list(map(int,input().split()))

def L(): return list(map(int,input().split()))

def l(): return list(map(int,input().split()))

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

sys.setrecursionlimit(10 ** 9)

INF = 10**9

mod = 10**9+7

N = i()

L = []

for i in range(N):

    A,B = I()

    L.append([B,A])

L.sort()

cnt = 0

for i in range(N):

    cnt += L[i][1]

    if cnt > L[i][0]:

        print('No')

        exit()

print('Yes')

from collections import deque

N = int(eval(input()))
A = deque()
for _ in range(N):
    a, b = list(map(int, input().split()))
    A.append([b, a])
A = list(A)
A.sort()
time = 0
for i in A:
    time += i[1]
    if time > i[0]:
        print("No")
        exit()
print("Yes")

import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque, Counter
from decimal import Decimal

def s():
    return eval(input())

def i():
    return int(eval(input()))

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

def I():
    return list(map(int, input().split()))

def L():
    return list(map(int, input().split()))

def l():
    return list(map(int, input().split()))

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

sys.setrecursionlimit(10**9)
INF = 10**9
mod = 10**9 + 7
N = i()
L = []
for i in range(N):
    A, B = I()
    L.append([B, A])
L.sort()
cnt = 0
for i in range(N):
    cnt += L[i][1]
    if cnt > L[i][0]:
        print("No")
        exit()
print("Yes")

-    a, b = list(map(int, input().split()))

+    return list(map(int, input().split()))


Input:
import itertools

N,M=list(map(int,input().split()))

ab=[]

for i in range(M):

    a,b=list(map(int,input().split()))

    ab.append([a,b])

per=[i for i in range(2,N+1)]

permu=[]

for i in itertools.permutations(per):

    lst=[1]

    lst.extend(i)

    permu.append(lst)

num=0

for lst in permu:

    can=True

    for i in range(N-1):

        if not([lst[i],lst[i+1]] in ab) and not([lst[i+1],lst[i]] in ab):

            can=False

            break

    if can:

        num+=1

print(num)

from itertools import permutations

from heapq import *

from collections import defaultdict

from math import log

from itertools import accumulate

from collections import deque

#maxv=[0]

import sys

sys.setrecursionlimit(100100)

import math

from math import ceil

from copy import deepcopy

N,M=list(map(int,input().split()))

T=[[] for i in range(N)]

for i in range(M):

    u,v=list(map(int,input().split()))

    u,v=u-1,v-1

    T[u].append(v)

    T[v].append(u)

list=list(range(1,N))

ans=0

for l in permutations(list):

    t=True

    for i in range(N-1):

        if i==0:

            if not l[0] in T[0]:

                t=False

                break

        else:

            if not l[i] in T[l[i-1]]:

                t=False

                break

    if t:

        ans+=1

print(ans)

import itertools

N, M = list(map(int, input().split()))
ab = []
for i in range(M):
    a, b = list(map(int, input().split()))
    ab.append([a, b])
per = [i for i in range(2, N + 1)]
permu = []
for i in itertools.permutations(per):
    lst = [1]
    lst.extend(i)
    permu.append(lst)
num = 0
for lst in permu:
    can = True
    for i in range(N - 1):
        if not ([lst[i], lst[i + 1]] in ab) and not ([lst[i + 1], lst[i]] in ab):
            can = False
            break
    if can:
        num += 1
print(num)

from itertools import permutations
from heapq import *
from collections import defaultdict
from math import log
from itertools import accumulate
from collections import deque

# maxv=[0]
import sys

sys.setrecursionlimit(100100)
import math
from math import ceil
from copy import deepcopy

N, M = list(map(int, input().split()))
T = [[] for i in range(N)]
for i in range(M):
    u, v = list(map(int, input().split()))
    u, v = u - 1, v - 1
    T[u].append(v)
    T[v].append(u)
list = list(range(1, N))
ans = 0
for l in permutations(list):
    t = True
    for i in range(N - 1):
        if i == 0:
            if not l[0] in T[0]:
                t = False
                break
        else:
            if not l[i] in T[l[i - 1]]:
                t = False
                break
    if t:
        ans += 1
print(ans)

-    a, b = list(map(int, input().split()))

+    u, v = list(map(int, input().split()))

-        if not ([lst[i], lst[i + 1]] in ab) and not ([lst[i + 1], lst[i]] in ab):

+            if not l[i] in T[l[i - 1]]:


Input:
import itertools

import sys

import math

import numpy as np

from collections import deque

from itertools import combinations

from functools import reduce

from functools import lru_cache

sys.setrecursionlimit(10**9)

def main():

    N = int(eval(input()))

    print((math.ceil(N/2)))

if __name__ == "__main__":

  main()

n = int(eval(input()))

if n%2==0:

  print((n//2))

else:

  print(((n+1)//2))

import itertools
import sys
import math
import numpy as np
from collections import deque
from itertools import combinations
from functools import reduce
from functools import lru_cache

sys.setrecursionlimit(10**9)

def main():
    N = int(eval(input()))
    print((math.ceil(N / 2)))

if __name__ == "__main__":
    main()

n = int(eval(input()))
if n % 2 == 0:
    print((n // 2))
else:
    print(((n + 1) // 2))

-import itertools
-import sys
-import math
-import numpy as np
-from collections import deque
-from itertools import combinations
-from functools import reduce
-from functools import lru_cache
-sys.setrecursionlimit(10**9)
-def main():
-    N = int(eval(input()))
-    print((math.ceil(N / 2)))
-if __name__ == "__main__":
-    main()
+n = int(eval(input()))
+if n % 2 == 0:
+    print((n // 2))
+else:
+    print(((n + 1) // 2))


Input:
n = int(eval(input()))

a = ([list(map(int, input().split())) for _ in range(n)])

a.insert(0,[0,0,0])

dp = [[0,0,0] for _ in range(n+1)]

for i, a in enumerate(a):

  dp_1 = dp[i-1]

  dp[i][0] = max(dp_1[1], dp_1[2]) + a[0]

  dp[i][1] = max(dp_1[0], dp_1[2]) + a[1]

  dp[i][2] = max(dp_1[0], dp_1[1]) + a[2]

print((max(dp[n])))

-a = [list(map(int, input().split())) for _ in range(n)]

+        abc.append(tuple(map(int, input().split())))


Input:
n = int(eval(input()))

m,a,r,c,h = 0,0,0,0,0

for i in range(n):

    s = input()[0]

    if s=='M':m+=1

    elif s=='A':a+=1

    elif s=='R':r+=1

    elif s=='C':c+=1

    elif s=='H':h+=1

x = [m,a,r,c,h]

ans = 0

for i in range(5):

    for j in range(i):

        for k in range(j):

                ans += x[i]*x[j]*x[k]

print(ans)

import itertools

from collections import Counter

n=int(eval(input()))

s=[input()[0] for _ in range(n)]

d=Counter(s)

ans=0

for i,j,k in itertools.combinations("MARCH",3):

    ans+=d[i]*d[j]*d[k]

print(ans)

n = int(eval(input()))
m, a, r, c, h = 0, 0, 0, 0, 0
for i in range(n):
    s = input()[0]
    if s == "M":
        m += 1
    elif s == "A":
        a += 1
    elif s == "R":
        r += 1
    elif s == "C":
        c += 1
    elif s == "H":
        h += 1
x = [m, a, r, c, h]
ans = 0
for i in range(5):
    for j in range(i):
        for k in range(j):
            ans += x[i] * x[j] * x[k]
print(ans)

import itertools
from collections import Counter

n = int(eval(input()))
s = [input()[0] for _ in range(n)]
d = Counter(s)
ans = 0
for i, j, k in itertools.combinations("MARCH", 3):
    ans += d[i] * d[j] * d[k]
print(ans)

+for i, j, k in itertools.combinations("MARCH", 3):


Input:
from itertools import combinations_with_replacement as comb_rplc

N,M,Q = list(map(int,input().split()))

array = [ list(map(int,input().split())) for _ in range(Q) ]

ans = 0

for seq in comb_rplc(list(range(1, M + 1)), N):

  score = 0

  for a,b,c,d in array:

    if seq[b-1] - seq [a-1] == c:

      score += d

  ans = max(score,ans)

print(ans)

from collections import deque

def calc(seq):

  score = 0

  for a,b,c,d in array:

    if seq[b-1] - seq[a-1] == c:

      score += d

  return score

N,M,Q = list(map(int,input().split()))

array = [ list(map(int,input().split())) for _ in range (Q) ]

ans = 0

que = deque()

for i in range(1,M+1):

  que.append([i])

while que:

  seq = que.popleft()

  if len(seq) == N:

    score = calc(seq)

    ans = max(ans,score)

  else:

    for i in range(seq[-1],M+1):

      seq_next = seq + [i]

      que.append(seq_next)

print(ans)

from itertools import combinations_with_replacement as comb_rplc

N, M, Q = list(map(int, input().split()))
array = [list(map(int, input().split())) for _ in range(Q)]
ans = 0
for seq in comb_rplc(list(range(1, M + 1)), N):
    score = 0
    for a, b, c, d in array:
        if seq[b - 1] - seq[a - 1] == c:
            score += d
    ans = max(score, ans)
print(ans)

from collections import deque

def calc(seq):
    score = 0
    for a, b, c, d in array:
        if seq[b - 1] - seq[a - 1] == c:
            score += d
    return score

N, M, Q = list(map(int, input().split()))
array = [list(map(int, input().split())) for _ in range(Q)]
ans = 0
que = deque()
for i in range(1, M + 1):
    que.append([i])
while que:
    seq = que.popleft()
    if len(seq) == N:
        score = calc(seq)
        ans = max(ans, score)
    else:
        for i in range(seq[-1], M + 1):
            seq_next = seq + [i]
            que.append(seq_next)
print(ans)

-from itertools import combinations_with_replacement as comb_rplc

-for seq in comb_rplc(list(range(1, M + 1)), N):

+        for i in range(seq[-1], M + 1):


Input:
import sys

sys.setrecursionlimit(10**7)

INF = 10 ** 18

MOD = 10 ** 9 + 7

def POW(x, y): return pow(x, y, MOD)

def INV(x, m=MOD): return pow(x, m - 2, m)

def DIV(x, y, m=MOD): return (x * INV(y, m)) % m

def LI(): return [int(x) for x in sys.stdin.readline().split()]

def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF(): return [float(x) for x in sys.stdin.readline().split()]

def LS(): return sys.stdin.readline().split()

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

def SI(): return eval(input())

# factorials

_nfact = 2 * (10 ** 5) + 10

facts = [0] * _nfact

facts[0] = 1

for i in range(1, _nfact):

    facts[i] = (facts[i - 1] * i) % MOD

def binomial(m, n):

    return DIV(facts[m], facts[n] * facts[m - n])

def main():

    N, A, B, C = LI()

    D = DIV(100, 100 - C)

    ans = 0

    ap, bp, abp = [1], [1], [POW(A + B, N)]

    for _ in range(0, N + 1):

        ap.append(ap[-1] * A % MOD)

        bp.append(bp[-1] * B % MOD)

        abp.append(abp[-1] * (A + B) % MOD)

    for m in range(N, 2 * N):

        x = binomial(m - 1, N - 1) * DIV(

            ap[N] * bp[m - N] + ap[m - N] * bp[N],

            abp[m - N]) % MOD

        y = (m * D) % MOD

        ans = (ans + (x * y) % MOD) % MOD

    return ans

print((main()))

import sys

sys.setrecursionlimit(10**7)

INF = 10 ** 18

MOD = 10 ** 9 + 7

def POW(x, y): return pow(x, y, MOD)

def INV(x, m=MOD): return pow(x, m - 2, m)

def DIV(x, y, m=MOD): return (x * INV(y, m)) % m

def LI(): return [int(x) for x in sys.stdin.readline().split()]

def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF(): return [float(x) for x in sys.stdin.readline().split()]

def LS(): return sys.stdin.readline().split()

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

def SI(): return eval(input())

# factorials

_nfact = 2 * (10 ** 5) + 10

facts = [0] * _nfact

facts[0] = 1

for i in range(1, _nfact):

    facts[i] = (facts[i - 1] * i) % MOD

ifacts = [0] * _nfact

ifacts[-1] = INV(facts[-1])

for i in range(_nfact - 1, 0, -1):

    ifacts[i - 1] = (ifacts[i] * i) % MOD

def binomial(m, n):

    return facts[m] * ifacts[n] * ifacts[m - n]

def main():

    N, A, B, C = LI()

    D = DIV(100, 100 - C)

    ans = 0

    ap, bp, abp = [1], [1], [POW(A + B, N)]

    for _ in range(0, N + 1):

        ap.append(ap[-1] * A % MOD)

        bp.append(bp[-1] * B % MOD)

        abp.append(abp[-1] * (A + B) % MOD)

    for m in range(N, 2 * N):

        x = binomial(m - 1, N - 1) * DIV(

            ap[N] * bp[m - N] + ap[m - N] * bp[N],

            abp[m - N]) % MOD

        y = (m * D) % MOD

        ans = (ans + (x * y) % MOD) % MOD

    return ans

print((main()))

import sys

sys.setrecursionlimit(10**7)
INF = 10**18
MOD = 10**9 + 7

def POW(x, y):
    return pow(x, y, MOD)

def INV(x, m=MOD):
    return pow(x, m - 2, m)

def DIV(x, y, m=MOD):
    return (x * INV(y, m)) % m

def LI():
    return [int(x) for x in sys.stdin.readline().split()]

def LI_():
    return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
    return [float(x) for x in sys.stdin.readline().split()]

def LS():
    return sys.stdin.readline().split()

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

def SI():
    return eval(input())

# factorials
_nfact = 2 * (10**5) + 10
facts = [0] * _nfact
facts[0] = 1
for i in range(1, _nfact):
    facts[i] = (facts[i - 1] * i) % MOD

def binomial(m, n):
    return DIV(facts[m], facts[n] * facts[m - n])

def main():
    N, A, B, C = LI()
    D = DIV(100, 100 - C)
    ans = 0
    ap, bp, abp = [1], [1], [POW(A + B, N)]
    for _ in range(0, N + 1):
        ap.append(ap[-1] * A % MOD)
        bp.append(bp[-1] * B % MOD)
        abp.append(abp[-1] * (A + B) % MOD)
    for m in range(N, 2 * N):
        x = (
            binomial(m - 1, N - 1)
            * DIV(ap[N] * bp[m - N] + ap[m - N] * bp[N], abp[m - N])
            % MOD
        )
        y = (m * D) % MOD
        ans = (ans + (x * y) % MOD) % MOD
    return ans

print((main()))

import sys

sys.setrecursionlimit(10**7)
INF = 10**18
MOD = 10**9 + 7

def POW(x, y):
    return pow(x, y, MOD)

def INV(x, m=MOD):
    return pow(x, m - 2, m)

def DIV(x, y, m=MOD):
    return (x * INV(y, m)) % m

def LI():
    return [int(x) for x in sys.stdin.readline().split()]

def LI_():
    return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
    return [float(x) for x in sys.stdin.readline().split()]

def LS():
    return sys.stdin.readline().split()

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

def SI():
    return eval(input())

# factorials
_nfact = 2 * (10**5) + 10
facts = [0] * _nfact
facts[0] = 1
for i in range(1, _nfact):
    facts[i] = (facts[i - 1] * i) % MOD
ifacts = [0] * _nfact
ifacts[-1] = INV(facts[-1])
for i in range(_nfact - 1, 0, -1):
    ifacts[i - 1] = (ifacts[i] * i) % MOD

def binomial(m, n):
    return facts[m] * ifacts[n] * ifacts[m - n]

def main():
    N, A, B, C = LI()
    D = DIV(100, 100 - C)
    ans = 0
    ap, bp, abp = [1], [1], [POW(A + B, N)]
    for _ in range(0, N + 1):
        ap.append(ap[-1] * A % MOD)
        bp.append(bp[-1] * B % MOD)
        abp.append(abp[-1] * (A + B) % MOD)
    for m in range(N, 2 * N):
        x = (
            binomial(m - 1, N - 1)
            * DIV(ap[N] * bp[m - N] + ap[m - N] * bp[N], abp[m - N])
            % MOD
        )
        y = (m * D) % MOD
        ans = (ans + (x * y) % MOD) % MOD
    return ans

print((main()))

+    ifacts[i - 1] = (ifacts[i] * i) % MOD

-    return DIV(facts[m], facts[n] * facts[m - n])

+    return facts[m] * ifacts[n] * ifacts[m - n]


Input:
n, m = list(map(int, input().split()))

p = set()

q = set()

for _ in range(m):

    a, b = list(map(int, input().split()))

    if a == 1:

        p.add(b)

    elif b == n:

        q.add(a)

else:

    print(("POSSIBLE" if p & q else "IMPOSSIBLE"))

n, m = input().split()

p = set()

q = set()

for _ in range(int(m)):

    a, b = input().split()

    if a == "1":

        p.add(b)

    elif b == n:

        q.add(a)

else:

    if p & q:

        print("POSSIBLE")

    else:

        print("IMPOSSIBLE")

-    a, b = list(map(int, input().split()))

-    print(("POSSIBLE" if p & q else "IMPOSSIBLE"))


Input: print((["Yes","No"][(lambda x:(abs(x//100-x%10)+99)//100)(eval(input()))]))


Input:
N = int(eval(input()))

A = [list(map(int, input().split())) for i in range(N)]

INF = 10 ** 9 + 1

for i in range(N):

    A[i][i] = INF

ans = 0

for i in range(N):

    Ai = A[i]

    for j in range(i):

        d = min(list(map(sum, list(zip(Ai, A[j])))))

        if A[i][j] > d:

            print('-1')

            exit()

        elif A[i][j] < d:

            ans += A[i][j]

print(ans)

N = int(eval(input()))

A = [list(map(int, input().split())) for i in range(N)]

# Warshall-Floyd法

dist = [list(A[i]) for i in range(N)]

via = [[None for i in range(N)] for j in range(N)]

for k in range(N):

    for i in range(N):

        for j in range(N):

            d = dist[i][k] + dist[k][j]

            if dist[i][j] >= d:

                dist[i][j] = d

                if i != k and j != k:

                    via[i][j] = k

ans = 0

for i in range(N):

    for j in range(i):

        if dist[i][j] < A[i][j]:

            ans = -1

            break

        if via[i][j] == None:

            ans += dist[i][j]

    if ans == -1:

        break

print(ans)

N = int(eval(input()))
A = [list(map(int, input().split())) for i in range(N)]
INF = 10**9 + 1
for i in range(N):
    A[i][i] = INF
ans = 0
for i in range(N):
    Ai = A[i]
    for j in range(i):
        d = min(list(map(sum, list(zip(Ai, A[j])))))
        if A[i][j] > d:
            print("-1")
            exit()
        elif A[i][j] < d:
            ans += A[i][j]
print(ans)

+via = [[None for i in range(N)] for j in range(N)]

+            d = dist[i][k] + dist[k][j]

-        d = min(list(map(sum, list(zip(Ai, A[j])))))


Input:
import sys

import numba as nb

import numpy as np

input = sys.stdin.readline

@nb.njit("i8(i8[:], i8)")

def binary_search(a, key):

    """Meguru type binary search"""

    ng = -1

    ok = len(a)

    while (abs(ok - ng) > 1):

        mid = (ok + ng) // 2

        if a[mid] >= key:

            ok = mid

        else:

            ng = mid

    return ok

@nb.njit("i8(i8, i8[:])")

def solve(N, L):

    L.sort()

    ans = 0

    for a in range(N - 2):

        for b in range(a + 1, N - 1):

            c = binary_search(L, L[a] + L[b]) - 1

            ans += c - b

    return ans

def main():

    N = int(eval(input()))

    L = np.array(input().split(), dtype=np.int64)

    ans = solve(N, L)

    print(ans)

if __name__ == "__main__":

    main()

import sys

import numba as nb

import numpy as np

input = sys.stdin.readline

@nb.njit("i8(i8[:], i8)", cache=True)

def binary_search(a, key):

    """Meguru type binary search"""

    ng = -1

    ok = len(a)

    while (abs(ok - ng) > 1):

        mid = (ok + ng) // 2

        if a[mid] >= key:

            ok = mid

        else:

            ng = mid

    return ok

@nb.njit("i8(i8, i8[:])", cache=True)

def solve(N, L):

    L.sort()

    ans = 0

    for a in range(N - 2):

        for b in range(a + 1, N - 1):

            c = binary_search(L, L[a] + L[b]) - 1

            ans += c - b

    return ans

def main():

    N = int(eval(input()))

    L = np.array(input().split(), dtype=np.int64)

    ans = solve(N, L)

    print(ans)

if __name__ == "__main__":

    main()

import sys
import numba as nb
import numpy as np

input = sys.stdin.readline

@nb.njit("i8(i8[:], i8)")
def binary_search(a, key):
    """Meguru type binary search"""
    ng = -1
    ok = len(a)
    while abs(ok - ng) > 1:
        mid = (ok + ng) // 2
        if a[mid] >= key:
            ok = mid
        else:
            ng = mid
    return ok

@nb.njit("i8(i8, i8[:])")
def solve(N, L):
    L.sort()
    ans = 0
    for a in range(N - 2):
        for b in range(a + 1, N - 1):
            c = binary_search(L, L[a] + L[b]) - 1
            ans += c - b
    return ans

def main():
    N = int(eval(input()))
    L = np.array(input().split(), dtype=np.int64)
    ans = solve(N, L)
    print(ans)

if __name__ == "__main__":
    main()

import sys
import numba as nb
import numpy as np

input = sys.stdin.readline

@nb.njit("i8(i8[:], i8)", cache=True)
def binary_search(a, key):
    """Meguru type binary search"""
    ng = -1
    ok = len(a)
    while abs(ok - ng) > 1:
        mid = (ok + ng) // 2
        if a[mid] >= key:
            ok = mid
        else:
            ng = mid
    return ok

@nb.njit("i8(i8, i8[:])", cache=True)
def solve(N, L):
    L.sort()
    ans = 0
    for a in range(N - 2):
        for b in range(a + 1, N - 1):
            c = binary_search(L, L[a] + L[b]) - 1
            ans += c - b
    return ans

def main():
    N = int(eval(input()))
    L = np.array(input().split(), dtype=np.int64)
    ans = solve(N, L)
    print(ans)

if __name__ == "__main__":
    main()


Input:
import sys

input = sys.stdin.readline

sys.setrecursionlimit(10**7)

def dfs(v, c, C, visited, G):

    visited[v] = True

    C[v] = c

    for nv, w in G[v]:

        if visited[nv]:

            continue

        dfs(nv, c^w, C, visited, G)

def main():

    N = int(eval(input()))

    G = [[] for _ in range(N)]

    for _ in range(N-1):

        u, v, w = list(map(int, input().split()))

        G[u-1].append([v-1, w%2])

        G[v-1].append([u-1, w%2])

    # print(G)

    C = [0]*N

    visited = [False]*N

    dfs(0, 0, C, visited, G)

    for c in C:

        print(c)

if __name__ == '__main__':

    main()

from functools import reduce

from fractions import gcd

import math

import bisect

import itertools

import sys

sys.setrecursionlimit(10**7)

input = sys.stdin.readline

INF = float("inf")

MOD = 1000000007

class Edge:

    def __init__(self, to, cost):

        self.to = to

        self.cost = cost

visited = []

vertex = []

def dfs(G, v, d):

    visited[v] = True

    if d % 2 == 0:

        vertex[v] = 0

    else:

        vertex[v] = 1

    for edge in G[v]:

        nv = edge.to

        if visited[nv]:

            continue

        nd = d + edge.cost

        dfs(G, nv, nd)

def main():

    N = int(eval(input()))

    G = [[] for _ in range(N)]

    for _ in range(N-1):

        a, b, cost = list(map(int, input().split()))

        a -= 1; b -= 1

        G[a].append(Edge(b, cost))

        G[b].append(Edge(a, cost))

    for _ in range(N):

        visited.append(False)

        vertex.append(0)

    dfs(G, 0, 0)

    for v in vertex:

        print(v)

if __name__ == '__main__':

    main()

import sys

input = sys.stdin.readline
sys.setrecursionlimit(10**7)

def dfs(v, c, C, visited, G):
    visited[v] = True
    C[v] = c
    for nv, w in G[v]:
        if visited[nv]:
            continue
        dfs(nv, c ^ w, C, visited, G)

def main():
    N = int(eval(input()))
    G = [[] for _ in range(N)]
    for _ in range(N - 1):
        u, v, w = list(map(int, input().split()))
        G[u - 1].append([v - 1, w % 2])
        G[v - 1].append([u - 1, w % 2])
    # print(G)
    C = [0] * N
    visited = [False] * N
    dfs(0, 0, C, visited, G)
    for c in C:
        print(c)

if __name__ == "__main__":
    main()

from functools import reduce
from fractions import gcd
import math
import bisect
import itertools
import sys

sys.setrecursionlimit(10**7)
input = sys.stdin.readline
INF = float("inf")
MOD = 1000000007

class Edge:
    def __init__(self, to, cost):
        self.to = to
        self.cost = cost

visited = []
vertex = []

def dfs(G, v, d):
    visited[v] = True
    if d % 2 == 0:
        vertex[v] = 0
    else:
        vertex[v] = 1
    for edge in G[v]:
        nv = edge.to
        if visited[nv]:
            continue
        nd = d + edge.cost
        dfs(G, nv, nd)

def main():
    N = int(eval(input()))
    G = [[] for _ in range(N)]
    for _ in range(N - 1):
        a, b, cost = list(map(int, input().split()))
        a -= 1
        b -= 1
        G[a].append(Edge(b, cost))
        G[b].append(Edge(a, cost))
    for _ in range(N):
        visited.append(False)
        vertex.append(0)
    dfs(G, 0, 0)
    for v in vertex:
        print(v)

if __name__ == "__main__":
    main()

-        u, v, w = list(map(int, input().split()))

+        a, b, cost = list(map(int, input().split()))