Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

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

fromR = {i:[] for i in range(N+1)}
toR = {i:[] for i in range(N+1)}

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

for _ in range(N-1+M):
    a,b=map(int,input().split())
    fromR[a].append([b,a])
    toR[b].append(a)

r=1
while len(toR[r])>0:
    r=toR[r][0]

ans[r]=0

que = fromR[r]

while len(que)>0:
    tmp=[]
    for q in que:
        b,a=q
        flag=True
        for p in toR[b]:
            flag = flag and ans[p]!=-1
            if flag==False:
                break
        if flag:
            ans[b]=a
            tmp+=fromR[b]
    que=tmp
for a in ans[1:]:
    print(a)


### FIXED CODE ###

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

fromR = {i:[] for i in range(N+1)}

cnt = [0 for i in range(N+1)]
ans = [-1 for i in range(N+1)]

for _ in range(N-1+M):
    a,b=map(int,input().split())
    fromR[a].append([b,a])
    cnt[b]+=1
r=-1
for i in range(1,N+1):
    if cnt[i]==0:
        r=i
        break

ans[r]=0

que = fromR[r]

while len(que)>0:
    tmp=[]
    for q in que:
        b,a=q
        cnt[b]-=1
        if cnt[b]==0:
            ans[b]=a
            tmp+=fromR[b]
    que=tmp
for a in ans[1:]:
    print(a)
### BUGGY CODE ###

import collections

n, m = map(int, input().split())
graph = [[] for i in range(n + 1)]
for i in range(m):
    l, r, d = map(int, input().split())
    graph[l].append([r, d])
    graph[r].append([l, -d])
flags = [False for i in range(n + 1)]
place = [None for i in range(n + 1)]
num = 0
flag = True
while num <= n:
    if flag == False:
        break
    if flags[num] == True:
        num += 1
        continue
    if graph[num] == []:
        place[num] = 0
        flags[num] = True
        num += 1
        continue
    place[num] = 0
    queue = collections.deque([num])
    while queue:
        test = queue.popleft()
        flags[test] = True
        for i in graph[test]:
            if flags[i[0]] == True:
                if place[i[0]] - place[test] != i[1]:
                    flag = False
                    break
            else:
                place[i[0]] = place[test] + i[1]
                queue.append(i[0])
    else:
        num += 1
if flag == True:
    print("Yes")
else:
    print("No")

### FIXED CODE ###

import collections, sys
input = sys.stdin.readline

n, m = map(int, input().split())
graph = [[] for i in range(n + 1)]
for i in range(m):
    l, r, d = map(int, input().split())
    graph[l].append([r, d])
    graph[r].append([l, -d])
place = [None for i in range(n + 1)]

num = 0
flag = True
while num <= n:
    if flag == False:
        break
    if place[num] != None:
        num += 1
        continue
    place[num] = 0
    queue = collections.deque([num])
    while queue:
        if flag == False:
            break
        test = queue.popleft()
        for i in graph[test]:
            if place[i[0]] != None:
                if place[i[0]] - place[test] != i[1]:
                    flag = False
                    break
            else:
                place[i[0]] = place[test] + i[1]
                queue.append(i[0])
    else:
        num += 1
if flag == True:
    print("Yes")
else:
    print("No")
### BUGGY CODE ###

s = input()
t = input()

def count_ans(a,b):    
    ans = 0
    for k in range(len(a)):
        for l in range(len(b)):
            if k==l and a[k]==b[l]:
                ans += 1
    return ans

ans = 0
for i in range(len(s)-len(t)+1):
    ans_ = count_ans(s[i:i+len(t)],t)
    if ans < ans_:
        ans = ans_

print(len(t)-ans)

### FIXED CODE ###

s = input()
t = input()

def count_ans(a,b):
    ans = 0
    for c1,c2 in zip(a, b):
        if c1 == c2:
            ans += 1
    return ans

ans = 0
for i in range(len(s)-len(t)+1):
    ans_ = count_ans(s[i:i+len(t)],t)
    if ans < ans_:
        ans = ans_

print(len(t)-ans)
### BUGGY CODE ###


from functools import lru_cache
import sys
sys.setrecursionlimit(10**9)
m=10**9+7

def len_max(mat):
    for i in range(len(mat)):
        if len(mat[i])>1:
            return i

n = int(input())
adj_mat =[[] for i in range(n)]



for i in range(n-1):
    a, b = [int(x) for x in input().strip().split()]
    adj_mat[a-1].append(b-1)
    adj_mat[b-1].append(a-1)

root = len_max(adj_mat) or 0


@lru_cache(None)
def cal(parent, node, col):

    nei = adj_mat[node]
    if len(nei)==1:
        if col=='w':
            return 2
        else:
            return 1 
    
    else:
        
        if col=='w':
            sb=1
            sw=1
            for k in nei:
                if k==parent:
                    continue
                sw*=cal(node, k, 'w')%m
                sb*=cal(node, k, 'b')%m
            return (sb+sw)%m

        else:
            sw=1
            for k in nei:
                if k==parent:
                    continue
                sw*=cal(node, k, 'w')%m
            return sw


ans = cal(None,root,'w')
if n==0:
    print(0)
elif n==1:
    print(2)
elif n==2:
    print(3)
else:
    print(ans%m)


### FIXED CODE ###

import sys
sys.setrecursionlimit(10**9)
m=10**9+7

def len_max(mat):
    for i in range(len(mat)):
        if len(mat[i])>1:
            return i

n = int(input())
adj_mat =[[] for i in range(n)]



for i in range(n-1):
    a, b = [int(x) for x in input().strip().split()]
    adj_mat[a-1].append(b-1)
    adj_mat[b-1].append(a-1)

root = len_max(adj_mat) or 0


dp=[{'w':None, 'b':None} for i in range(n)]



def cal(parent, node, col):

    nei = adj_mat[node]
    if len(nei)==1:
        if col=='w':
            return 2
        else:
            return 1 
    
    else:
        if dp[node][col]:
            return dp[node][col]
        
        if col=='w':
            sb=1
            sw=1
            for k in nei:
                if k==parent:
                    continue
                sw*=cal(node, k, 'w')%m
                sb*=cal(node, k, 'b')%m
            dp[node][col] = (sb+sw)%m
            return dp[node][col]

        else:
            sw=1
            for k in nei:
                if k==parent:
                    continue
                sw*=cal(node, k, 'w')%m
            dp[node][col] = sw
            return dp[node][col]


ans = cal(None,root,'w')
if n==0:
    print(0)
elif n==1:
    print(2)
elif n==2:
    print(3)
else:
    print(ans%m)
### BUGGY CODE ###

n, k = map(int, input().split())
p = [0] + list(map(int, input().split()))
c = [0] + list(map(int, input().split()))

maximum = max(c[1:])
if maximum < 0:
    print(maximum)
    exit()

used = [False] * (n+1)

ans = 0

for i in range(1, n+1):
    if used[i]:continue
    loop = [i]
    now = i
    while p[now] != i:
        now = p[now]
        loop.append(now)

    L = len(loop)
    S = sum(c[v] for v in loop)
    for a_ind in range(L):
        a = loop[a_ind]
        used[a] = True
        dist_ab = 0
        cost_ab = -c[a]
        for b_ind in range(a_ind, a_ind+L):
            b = loop[b_ind % L]
            cost_ab += c[b]
            max_cost = cost_ab + max(0, S) * ((k - dist_ab) // L)
            ans = max(ans, max_cost)
            dist_ab += 1
            if dist_ab > k:
                break

print(ans)

### FIXED CODE ###

n, k = map(int, input().split())
p = [0] + list(map(int, input().split()))
c = [0] + list(map(int, input().split()))

maximum = max(c[1:])
if maximum < 0:
    print(maximum)
    exit()

used = [False] * (n+1)

ans = 0

for i in range(1, n+1):
    if used[i]:continue
    loop = [i]
    now = i
    while p[now] != i:
        now = p[now]
        loop.append(now)

    L = len(loop)
    S = sum(c[v] for v in loop)
    for a_ind in range(L):
        a = loop[a_ind]
        used[a] = True
        dist_ab = 0
        cost_ab = -c[a]
        for b_ind in range(a_ind, a_ind+L):
            b = loop[b_ind % L]
            cost_ab += c[b]
            max_cost = cost_ab + max(0, S) * ((k - dist_ab) // L)
            ans = max(ans, max_cost)
            dist_ab += 1
            if dist_ab > k:
                break

print(ans)
### BUGGY CODE ###

N = int(input())
a = ""
student = list(map(int,input().split()))
for i in range(N):
    a += " " + str(student.index(i+1) + 1)
a = list(a.split(" "))
a.pop(0)
print(" ".join(a))

### FIXED CODE ###

N = int(input())
student = list(map(int,input().split()))
A = [0] * N
for i in range(N):
    A[student[i]-1] = i+1
A = list(map(str,A))
print(" ".join(A))
### BUGGY CODE ###

K, S = list(map(int, input().rstrip().split()))

n = 0
for x in range(K + 1):
    for y in range(K + 1):
        for z in range(K + 1):
            if (x + y + z) == S:
                n += 1

print(n)


### FIXED CODE ###

K, S = list(map(int, input().rstrip().split()))

n = 0
for x in range(K + 1):
    for y in range(K + 1):
        z = S - x - y
        if 0 <= z and z <= K:
            n += 1

print(n)
### BUGGY CODE ###

N,K=map(int,input().split())
x=[0 for i in range(N)]
y=[0 for i in range(N)]
for i in range(N):
    a,b,c=map(str,input().split())
    a=int(a)
    b=int(b)
    if c=="W":
        b+=K
    x[i]=a%(2*K)
    y[i]=b%(2*K)
board=[[0 for i in range(2*K+1)] for i in range(2*K+1)]
for i in range(N):
    if x[i]>=K and y[i]>=K:
        x[i]-=K
        y[i]-=K
    if x[i]<K and y[i]>=K:
        x[i]+=K
        y[i]-=K
    if x[i]>=K and y[i]<K:
        board[0][y[i]+1]+=1
        board[0][y[i]+K+1]-=1
        board[x[i]-K+1][y[i]+1]-=2
        board[x[i]-K+1][y[i]+K+1]+=2
        board[x[i]-K+1][0]+=1
        board[x[i]+1][0]-=1
        board[x[i]+1][y[i]+1]+=2
        board[x[i]+1][y[i]+K+1]-=2
    if x[i]<K and y[i]<K:
        board[x[i]+1][y[i]+1]+=2
        board[x[i]+K+1][y[i]+K+1]+=2
        board[x[i]+1][y[i]+K+1]-=2
        board[x[i]+K+1][y[i]+1]-=2
        board[0][0]+=1
        board[x[i]+1][0]-=1
        board[0][y[i]+1]-=1
        board[x[i]+K+1][0]+=1
        board[0][y[i]+K+1]+=1
tmp=[[0 for i in range(2*K)] for i in range(2*K)]
bs=[[0 for i in range(2*K)] for i in range(2*K)]
for i in range(2*K):
    for j in range(2*K):
        if i==0:
            tmp[i][j]=board[i][j]
        else:
            tmp[i][j]=board[i][j]+tmp[i-1][j]
for i in range(2*K):
    for j in range(2*K):
        if j==0:
            bs[i][j]=tmp[i][j]
        else:
            bs[i][j]=tmp[i][j]+bs[i][j-1]
ans=bs[0][0]
for i in range(2*K):
    for j in range(2*K):
        if bs[i][j]>ans:
            ans=bs[i][j]
print(ans)


### FIXED CODE ###

N,K=map(int,input().split())
x=[0 for i in range(N)]
y=[0 for i in range(N)]
for i in range(N):
    a,b,c=map(str,input().split())
    a=int(a)
    b=int(b)
    if c=="W":
        b+=K
    x[i]=a%(2*K)
    y[i]=b%(2*K)
board=[[0 for i in range(2*K+1)] for i in range(2*K+1)]
for i in range(N):
    if x[i]>=K and y[i]>=K:
        x[i]-=K
        y[i]-=K
    if x[i]<K and y[i]>=K:
        x[i]+=K
        y[i]-=K
    if x[i]>=K and y[i]<K:
        board[0][y[i]+1]+=1
        board[0][y[i]+K+1]-=1
        board[x[i]-K+1][y[i]+1]-=2
        board[x[i]-K+1][y[i]+K+1]+=2
        board[x[i]-K+1][0]+=1
        board[x[i]+1][0]-=1
        board[x[i]+1][y[i]+1]+=2
        board[x[i]+1][y[i]+K+1]-=2
    if x[i]<K and y[i]<K:
        board[x[i]+1][y[i]+1]+=2
        board[x[i]+K+1][y[i]+K+1]+=2
        board[x[i]+1][y[i]+K+1]-=2
        board[x[i]+K+1][y[i]+1]-=2
        board[0][0]+=1
        board[x[i]+1][0]-=1
        board[0][y[i]+1]-=1
        board[x[i]+K+1][0]+=1
        board[0][y[i]+K+1]+=1
tmp=[[0 for i in range(2*K)] for i in range(2*K)]
bs=[[0 for i in range(2*K)] for i in range(2*K)]
for i in range(2*K):
    for j in range(2*K):
        if i==0:
            tmp[i][j]=board[i][j]
        else:
            tmp[i][j]=board[i][j]+tmp[i-1][j]
for i in range(2*K):
    for j in range(2*K):
        if j==0:
            bs[i][j]=tmp[i][j]
        else:
            bs[i][j]=tmp[i][j]+bs[i][j-1]
ans=bs[0][0]
for i in range(2*K):
    for j in range(2*K):
        if bs[i][j]>ans:
            ans=bs[i][j]
print(ans)
### BUGGY CODE ###

n,*a=map(int,open(0).read().split());c=[0]+a+[0]
for i in range(n):s,t,r=c[i:i+3];print(sum(abs(c[t]-c[t+1])for t in range(n+1))+abs(r-s)-abs(t-s)-abs(t-r))

### FIXED CODE ###

n,*a=map(int,open(0).read().split())
c=[0]+a+[0]
y=sum(abs(c[t]-c[t+1])for t in range(n+1))
for i in range(n):
  s,t,r=c[i:i+3]
  print(y+abs(r-s)-abs(t-s)-abs(t-r))
### BUGGY CODE ###

from heapq import heappush, heappop

n,m,s,t = map(int,input().split())
e = [[] for i in range(n+1)]
for i in range(m):
    u,v,a,b = map(int,input().split())
    e[u].append((v,a,b))
    e[v].append((u,a,b))

S = [float("INF")]*(n+1)
S[s] = 0
h = []
heappush(h,[s,0])
while h:
    now,d = heappop(h)
    for nex,a,b in e[now]:
        if d+a < S[nex]:
            S[nex] = d+a
            heappush(h,[nex,d+a])
T = [float("INF")]*(n+1)
T[t] = 0
h = []
heappush(h,[t,0])
while h:
    now,d = heappop(h)
    for nex,a,b in e[now]:
        if d+b < T[nex]:
            T[nex] = d+b
            heappush(h,[nex,d+b])

ans = [0]*(n+1)
cost = 10**15
now = 0
for i in range(n,0,-1):
    now = max(now,cost-S[i]-T[i])
    ans[i] = now
for i in ans[1:]:
    print(i)

### FIXED CODE ###

from collections import deque

n,m,s,t = map(int,input().split())
e = [[] for i in range(n+1)]
for i in range(m):
    u,v,a,b = map(int,input().split())
    e[u].append((v,a,b))
    e[v].append((u,a,b))
S = [float("INF")]*(n+1)
S[s] = 0
q = deque([])
q.append([s,-1])
while q:
    now,bef = q.popleft()
    for nex,a,b in e[now]:
        if nex == bef:
            continue
        if S[now]+a < S[nex]:
            S[nex] = S[now]+a
            q.append([nex,now])
T = [float("INF")]*(n+1)
T[t] = 0
q = deque([])
q.append([t,-1])
while q:
    now,bef = q.popleft()
    for nex,a,b in e[now]:
        if nex == bef:
            continue
        if T[now]+b < T[nex]:
            T[nex] = T[now]+b
            q.append([nex,now])

ans = [0]*(n+1)
cost = 10**15
now = 0
for i in range(n,0,-1):
    now = max(now,cost-S[i]-T[i])
    ans[i] = now
for i in ans[1:]:
    print(i)
### BUGGY CODE ###

"""
最小公倍数LCMを求めて、LCMを各Aで除した商を合計すればよい
と思ったけど、普通に最小公倍数を求めると、値が大きくなりすぎて処理できない。
なので、積は求めずに、素因数の足し引きだけ処理する。
"""
from math import gcd
from collections import Counter
mod = 10**9 +7
N = int(input())
A = list(map(int,input().split()))
factors = []
for i in range(N):
    a = A[i]
    tmp = []
    ceil = int(a**0.5)+1
    for d in range(2,ceil):
        for _ in range(100):
            if a%d==0:
                a//=d
                tmp.append(d)
            else:
                break
    if a != 1:
        tmp.append(a)
    factors.append(Counter(tmp))
    if i == 0:
        l = Counter(tmp)

for i in range(1,N):
    diff = factors[i]-l
    l += diff

ans = 0
for factor in factors:
    diff = l - factor
    tmp = 1
    for k,v in diff.items():
        tmp *= pow(k,v)
    ans += tmp
    ans %= mod

print(ans)

### FIXED CODE ###

from math import gcd

def lcm(a, b):
    return a*b//gcd(a,b)
def main():
    N = int(input())
    A = list(map(int, input().split()))
    mod = 10**9 + 7
    LCM = 1
    for a in A:
        LCM = lcm(LCM, a)
    ans = 0
    for a in A:
        ans += (LCM//a)
        

    print(ans % mod)
main()
### BUGGY CODE ###

s = input()
q = s.count('?')
res = 0
mod = 10**9+7

def add(num):
    global res
    res += num
    res %= mod

for pivot in range(1,len(s)-1):
    # s[:pivot]とs[pivot+1:]に分割
    if s[pivot]=='B' or s[pivot]=='?':
        tmp = q - (s[pivot]=='?')
        s1,s2 = s[:pivot],s[pivot+1:]
        s1_a,s1_q = s1.count('A'),s1.count('?')
        s2_c,s2_q = s2.count('C'),s2.count('?')
        add(s1_a * s2_c * 3**tmp)           # A,C
        add(s1_a * s2_q * 3**(tmp-1))       # A,?
        add(s1_q * s2_c * 3**(tmp-1))       # ?,C
        add(s1_q * s2_q * 3**(tmp-2))       # ?,?

print(int(res))


### FIXED CODE ###

s = input()
q = s.count('?')
res = 0
mod = 10**9+7

""" 前処理 """
# ある地点(pivot)まで(から)のA(C),?の数
bp_A,bp_Q,ap_C,ap_Q = [0],[0],[0],[0]
for c in s:
    bp_A.append(bp_A[-1]+(c=='A'))
    bp_Q.append(bp_Q[-1]+(c=='?'))
for c in s[::-1]:
    ap_C.append(ap_C[-1]+(c=='C'))
    ap_Q.append(ap_Q[-1]+(c=='?'))
# 3の累乗のmod
three = [3]
i = 1
while i<q:
    three.append((three[-1]**2)%mod)
    i *= 2
""" ここまで """

# 3の累乗
def pow(num):
    if num<0:
        return 0
    elif num==0:
        return 1
    else:
        ans = 1
        for i,j in enumerate(three):
            if num&(1<<i):
                ans *= j
                ans %= mod
        return ans

for pivot in range(1,len(s)-1):
    if s[pivot]=='B' or s[pivot]=='?':
        tmp = q - (s[pivot]=='?')
        s1_a,s1_q = bp_A[pivot],bp_Q[pivot]
        s2_c,s2_q = ap_C[-pivot-2],ap_Q[-pivot-2]
        res += (s1_a*s2_c)%mod * pow(tmp)
        res += (s1_a*s2_q)%mod * pow(tmp-1)
        res += (s1_q*s2_c)%mod * pow(tmp-1)
        res += (s1_q*s2_q)%mod * pow(tmp-2)

print(int(res)%mod)
### BUGGY CODE ###

import fractions

n, m = map(int, input().split())
s = input()
t = input()

l = n*m//fractions.gcd(n, m)

s_idx = [i * l//n + 1 for i in range(n)]
t_idx = [i * l//m + 1 for i in range(m)]

dup_idx = set(s_idx) & set(t_idx)

for d in dup_idx:
    si = s_idx.index(d)
    ti = t_idx.index(d)
    if s[si] != t[ti]:
        print(-1)
        break
else:
    print(l)


### FIXED CODE ###

import fractions

n, m = map(int, input().split())
s = input()
t = input()

l = n*m//fractions.gcd(n, m)

s_dic = {i * l//n + 1: s[i] for i in range(n)}
t_dic = {i * l//m + 1: t[i] for i in range(m)}

dup_idx = set(s_dic.keys()) & set(t_dic.keys())

for d in dup_idx:
    if s_dic[d] != t_dic[d]:
        print(-1)
        break
else:
    print(l)
### BUGGY CODE ###

n = int(input())
li = list(map(int,input().split()))
count = 0
for i in range(n):
    if li.count(li[i]) >= 2:
        print("NO")
        break
    else:
        count += 1
if count == n:
    print("YES")

### FIXED CODE ###

n = int(input())
s = set(list(map(int,input().split())))

if n==len(s):
  print("YES")
else:
  print("NO")
### BUGGY CODE ###

N = int(input())
P = list(map(int,input().split()))
result = 0
for i in range(N):
    if P[i] == min(P[:i+1]):
        result += 1
print(result)

### FIXED CODE ###

N = int(input())
P = list(map(int,input().split()))
result = 0
nowmin = N
for i in range(N):
    if P[i]>nowmin:
        pass 
    else:
        result += 1
        nowmin = P[i]
print(result)
### BUGGY CODE ###

N = int(input())
A = []
for _ in range(N):
    A.append(int(input()))

for i in range(N):
    print(max(A[:i]+A[i+1:]))

### FIXED CODE ###

N = int(input())
A = []
for _ in range(N):
    A.append(int(input()))

max_A = max(A)
semimax_A = max_A if A.count(max_A) > 1 else list(sorted(list(set(A))))[-2]
for i in range(N):
    if A[i] == max_A:
        print(semimax_A)
    else:
        print(max_A)
### BUGGY CODE ###

import math
N=int(input())
for i in range(int(math.sqrt(N)),N+1):
    if N%i==0:
        print(len(str(i)))
        exit(0)

### FIXED CODE ###

N=int(input())
for i in range(int(N ** 0.5 + 1),-1,-1):
    if N % i == 0:
        print(len(str(N // i)))
        exit(0)
### BUGGY CODE ###

k,s=map(int,input().split())
res=0
for x in range(k+1):
  for y in range(k+1):
    for z in range(k+1):
      if x+y+z==s:
        res+=1
print(res)

### FIXED CODE ###

k,s=map(int,input().split())
res=0
for x in range(k+1):
  for y in range(k+1):
    if s-x-y<=k and s-x-y>=0:
      res+=1
      #print(x,y,s-x-y)
print(res)
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10**7)
input=sys.stdin.readline
h,w=map(int,input().split())
c=[int(i)-1 for i in input().split()]
d=[int(i)-1 for i in input().split()]
s=[input() for i in range(h)]
inf=1000000000000
dp=[[-1 if s[i][j]=="#" else inf for j in range(w)] for i in range(h)]
dp[c[0]][c[1]]=0
def bfs(i,j):
    global h,w,s,dp
    if i>0:
        if dp[i-1][j]>dp[i][j]:
            dp[i-1][j]=dp[i][j]
            bfs(i-1,j)
    if j>0:
        if dp[i][j-1]>dp[i][j]:
            dp[i][j-1]=dp[i][j]
            bfs(i,j-1)
    if i<h-1:
        if dp[i+1][j]>dp[i][j]:
            dp[i+1][j]=dp[i][j]
            bfs(i+1,j)
    if j<w-1:
        if dp[i][j+1]>dp[i][j]:
            dp[i][j+1]=dp[i][j]
            bfs(i,j+1)
    for k in range(-2,3):
        for l in range(-2,3):
            if 0<=i+k<h and 0<=j+l<w:
                if dp[i+k][j+l]>dp[i][j]+1:
                    dp[i+k][j+l]=dp[i][j]+1
                    bfs(i+k,j+l)

bfs(c[0],c[1])
print(dp[d[0]][d[1]] if dp[d[0]][d[1]]!=inf else -1)

### FIXED CODE ###

#辺の重み(変化量)が0である場合は01DFSって特別なやつ
#0の場合はそこでBFSできる
import sys
from collections import deque
sys.setrecursionlimit(10**7)
input=sys.stdin.readline
h,w=map(int,input().split())
c=[int(i)-1 for i in input().split()]
d=[int(i)-1 for i in input().split()]
s=[input() for i in range(h)]
inf=100000000
dp=[[-1 if s[i][j]=="#" else inf for j in range(w)] for i in range(h)]
dp[c[0]][c[1]]=0
now=deque([[c[0],c[1]]])
def bfs():
    global h,w,s,dp,now
    while len(now):
        i,j=now.popleft()
        if i>0:
            if dp[i-1][j]!=-1:
                if dp[i-1][j]>dp[i][j]:
                    dp[i-1][j]=dp[i][j]
                    now.appendleft([i-1,j])
        if j>0:
            if dp[i][j-1]!=-1:
                if dp[i][j-1]>dp[i][j]:
                    dp[i][j-1]=dp[i][j]
                    now.appendleft([i,j-1])
        if i<h-1:
            if dp[i+1][j]!=-1:
                if dp[i+1][j]>dp[i][j]:
                    dp[i+1][j]=dp[i][j]
                    now.appendleft([i+1,j])
        if j<w-1:
            if dp[i][j+1]!=-1:
                if dp[i][j+1]>dp[i][j]:
                    dp[i][j+1]=dp[i][j]
                    now.appendleft([i,j+1])
        for k in range(-2,3):
            for l in range(-2,3):
                if 0<=i+k<h and 0<=j+l<w:
                    if dp[i+k][j+l]!=-1:
                        if dp[i+k][j+l]>dp[i][j]+1:
                            dp[i+k][j+l]=dp[i][j]+1
                            now.append([i+k,j+l])
bfs()
print(dp[d[0]][d[1]] if dp[d[0]][d[1]]!=inf else -1)
### BUGGY CODE ###

import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, T = lr()
AB = [lr() for _ in range(N)]
AB.sort()
# 1つ除いたDP, DP[T-1]に除いたおいしさを足す
answer = 0
for i in range(N):
    dp = np.zeros(T, np.int64)
    for j in range(N):
        if j == i:
            continue
        sweet = AB[j][1]
        time = AB[j][0]
        dp[time:] = np.maximum(dp[time:], dp[:-time] + sweet)
    answer = max(answer, dp[-1] + AB[i][1])

print(answer)


### FIXED CODE ###

import sys
import numpy as np

n, t = map(int, input().split())
knapsack = np.zeros(t, dtype=np.int64)
cuisines = [tuple(map(int, line.split())) for line in sys.stdin]
cuisines.sort()
tmp_ans = 0
for a, b in cuisines:
    tmp_ans = max(tmp_ans, knapsack.max() + b)
    knapsack[a:] = np.maximum(knapsack[a:], knapsack[:-a] + b)
print(tmp_ans)
### BUGGY CODE ###

def main():
	L = input()
	n = len(L)
	MOD = 10**9+7
	if L == "0":
		print(1)
		return

	num_1 = 0
	pos = []
	for i in range(n):
		if L[i] == "1":
			num_1 += 1
			pos.append(n-i-1)
	def my_pow(x, n):
		if n == 0:
			return 1
		if n%2 == 0:
			return my_pow(x*x, n//2)
		return x * my_pow(x, n-1)

	ans = my_pow(2, num_1)%MOD
	for i, p in enumerate(pos):
		cur = my_pow(2, i)%MOD
		cur *= my_pow(3, p)%MOD
		cur %= MOD
		ans += cur
		ans %= MOD

	print(ans)


if __name__ == '__main__':
	main()

### FIXED CODE ###

MOD = 10**9+7

memo = dict()
memo[0] = 1
def pow3(n):
	if n in memo:
		return memo[n]
	if n%2 == 0:
		tmp = pow3(n//2)
		memo[n] = tmp*tmp%MOD
		return memo[n]
	memo[n] = 3 * pow3(n-1)%MOD
	return memo[n]

def cul(i, p):
	cur = (1<<i)%MOD
	cur *= pow3(p)
	return cur % MOD

def solve(L):
	n = len(L)
	if L == "0":
		return 1
	if not "0" in L:
		return pow3(n)

	num_1 = 0
	pos = []
	for i in range(n):
		if L[i] == "1":
			num_1 += 1
			pos.append(n-i-1)

	ans = (2**num_1)%MOD
	for i, p in enumerate(pos):
		ans += cul(i, p)
		ans %= MOD

	return ans

def main():
	L = input()
	print(solve(L))


if __name__ == '__main__':
	main()