1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
import sys,os import random from math import inf,ceil,sqrt from string import ascii_lowercase,ascii_uppercase from collections import Counter, defaultdict, deque from itertools import accumulate, combinations, permutations from heapq import heappushpop, heapify, heappop, heappush from bisect import bisect_left,bisect_right from types import GeneratorType from random import randint RANDOM = randint(1, 10 ** 10) class op(int): def __init__(self, x): int.__init__(x) def __hash__(self): return super(op, self).__hash__() ^ RANDOM def bootstrap(f, stack=[]): def wrappedfunc(*args, **kwargs): if stack: return f(*args, **kwargs) else: to = f(*args, **kwargs) while True: if type(to) is GeneratorType: stack.append(to) to = next(to) else: stack.pop() if not stack: break to = stack[-1].send(to) return to return wrappedfunc input = lambda: sys.stdin.buffer.readline().decode().strip() out = sys.stdout.write def S(): return input() def I(): return int(input()) def MI(): return map(int, input().split()) def MF(): return map(float, input().split()) def MS(): return input().split() def LS(): return list(input().split()) def LI(): return list(map(int,input().split())) def print1(x): return out(str(x)+"\n") def print2(x,y): return out(str(x)+" "+str(y)+"\n") def print3(x,y,z): return out(str(x)+" "+str(y)+" "+str(z)+"\n") def Query(i,j): print3('?', i, j) sys.stdout.flush() qi=I() return qi def print_arr(arr): out(' '.join(map(str,arr))) out(str("\n"))
mod = 10**9 + 7 N = 5*(10**5)+1 inv = lambda x:pow(x,mod-2,mod) jc = [1]*N jc[0] = 0 for i in range(2,N): jc[i] = i*jc[i-1] % mod
def solve(): n = I() a = [0]+LI() b = [0]+LI() w = [0]+LI() g = [[] for _ in range(n+1)] li = [0]*(n+1) deg = [0]*(n+1) for i in range(1,n+1): if a[i] >= a[b[i]] and a[i] < a[b[i]] + w[b[i]]: g[b[i]].append(i) deg[i] += 1 if a[i] < a[b[i]]: li[i] = 1 q = deque() for i in range(1,n+1): if deg[i] == 0: q.append(i) while q: x = q.popleft() if li[x] == 0: continue for y in g[x]: li[y] += li[x]+1 deg[y] -= 1 if deg[y] == 0: q.append(y)
for i in range(1,n+1): a[i] = (a[i] + w[i]*inv(jc[li[i]])) % mod print_arr(a[1:])
for _ in range(int(input())): solve()
|