Natasha travels around Mars in the Mars rover. But suddenly it broke down, namely — the logical scheme inside it. The scheme is an undirected tree (connected acyclic graph) with a root in the vertex $1$, in which every leaf (excluding root) is an input, and all other vertices are logical elements, including the root, which is output. One bit is fed to each input. One bit is returned at the output.

There are four types of logical elements: AND ($2$ inputs), OR ($2$ inputs), XOR ($2$ inputs), NOT ($1$ input). Logical elements take values from their direct descendants (inputs) and return the result of the function they perform. Natasha knows the logical scheme of the Mars rover, as well as the fact that only one input is broken. In order to fix the Mars rover, she needs to change the value on this input.

For each input, determine what the output will be if Natasha changes this input.


-----Input-----

The first line contains a single integer $n$ ($2 \le n \le 10^6$) — the number of vertices in the graph (both inputs and elements).

The $i$-th of the next $n$ lines contains a description of $i$-th vertex: the first word "AND", "OR", "XOR", "NOT" or "IN" (means the input of the scheme) is the vertex type. If this vertex is "IN", then the value of this input follows ($0$ or $1$), otherwise follow the indices of input vertices of this element: "AND", "OR", "XOR" have $2$ inputs, whereas "NOT" has $1$ input. The vertices are numbered from one.

It is guaranteed that input data contains a correct logical scheme with an output produced by the vertex $1$.


-----Output-----

Print a string of characters '0' and '1' (without quotes) — answers to the problem for each input in the ascending order of their vertex indices.


-----Example-----
Input
10
AND 9 4
IN 1
IN 1
XOR 6 5
AND 3 7
IN 0
NOT 10
IN 1
IN 1
AND 2 8

Output
10110


-----Note-----

The original scheme from the example (before the input is changed):

[Image]

Green indicates bits '1', yellow indicates bits '0'.

If Natasha changes the input bit $2$ to $0$, then the output will be $1$.

If Natasha changes the input bit $3$ to $0$, then the output will be $0$.

If Natasha changes the input bit $6$ to $1$, then the output will be $1$.

If Natasha changes the input bit $8$ to $0$, then the output will be $1$.

If Natasha changes the input bit $9$ to $0$, then the output will be $0$.

Difficulty: competition

Starter code:

Solutions:
import sys
n=int(input())
f={
 'AND':(lambda a:a[0]&a[1]),
 'OR':(lambda a:a[0]|a[1]),
 'XOR':(lambda a:a[0]^a[1]),
 'NOT':(lambda a:a[0]^1),
}
g={'0':(lambda a:0), '1':(lambda a:1)}
d=[(g[v[0]],[]) if o=='IN' else (f[o],[int(a)-1 for a in v]) for o,*v in map(str.split,sys.stdin.read().strip().split('\n'))]
t=[0]
for i in t:
 t.extend(d[i][1])
v=[0 for _ in range(n)]
for i in t[::-1]:
 o,a=d[i]
 v[i]=o([v[x] for x in a])
f=[0 for _ in range(n)]
f[0]=1
for i in t:
 if f[i]<1: continue
 o,a=d[i]
 b=[v[x]for x in a]
 assert o(b)==v[i]
 for j,k in enumerate(a):
  b[j]^=1
  f[k]=(o(b)!=v[i])
  b[j]^=1
print(''.join(str(f[i]^v[0]) for i in range(n) if not d[i][1]))

import sys
n=int(input())
f={'AND':(lambda a,b:a&b),'OR':(lambda a,b:a|b),'XOR':(lambda a,b:a^b),'NOT':(lambda a:a^1)}
g={'0':(lambda:0),'1':(lambda:1)}
d=[(g[v[0]],[]) if o=='IN' else (f[o],[int(a)-1 for a in v]) for o,*v in map(str.split,sys.stdin.read().strip().split('\n'))]
t=[0]
for i in t:t.extend(d[i][1])
v=[0]*n
for i in t[::-1]:v[i]=d[i][0](*(v[x] for x in d[i][1]))
f=[0]*n
f[0]=1
for i in t:
 if f[i]<1: continue
 o,a=d[i]
 b=[v[x]for x in a]
 assert o(*b)==v[i]
 for j,k in enumerate(a):
  b[j]^=1
  f[k]=(o(*b)!=v[i])
  b[j]^=1
print(''.join(str(f[i]^v[0]) for i in range(n) if not d[i][1]))

import sys
n=int(input())
f={'AND':(lambda a,b:a&b),'OR':(lambda a,b:a|b),'XOR':(lambda a,b:a^b),'NOT':(lambda a:a^1)}
g={'0':(lambda:0),'1':(lambda:1)}
d=[(g[v[0]],[]) if o=='IN' else (f[o],[int(a)-1 for a in v]) for o,*v in map(str.split,sys.stdin.read().strip().split('\n'))]
t=[0]
for i in t:t.extend(d[i][1])
v=[0]*n
for i in t[::-1]:v[i]=d[i][0](*(v[x] for x in d[i][1]))
f=[0]*n
f[0]=1
for i in t:
 if f[i]<1: continue
 o,a=d[i]
 for k in a:
  v[k]^=1
  f[k]=(o(*(v[x] for x in a))!=v[i])
  v[k]^=1
print(''.join(str(f[i]^v[0]) for i in range(n) if not d[i][1]))

import sys
n=int(input())
f={'AND':(lambda a,b:a&b),'OR':(lambda a,b:a|b),'XOR':(lambda a,b:a^b),'NOT':(lambda a:a^1)}
g={'0':(lambda:0),'1':(lambda:1)}
d=[(g[v[0]],[]) if o=='IN' else (f[o],[int(a)-1 for a in v]) for o,*v in map(str.split,sys.stdin.read().strip().split('\n'))]
q=lambda i:d[i][0](*(v[x] for x in d[i][1]))
t=[0]
for i in t:t.extend(d[i][1])
v=[0]*n
for i in t[::-1]:v[i]=q(i)
f=[0]*n
f[0]=1
for i in t:
 if f[i]<1: continue
 for k in d[i][1]:
  v[k]^=1
  f[k]=q(i)!=v[i]
  v[k]^=1
print(''.join(str(f[i]^v[0]) for i in range(n) if not d[i][1]))

import sys
n=int(input())
f={'AND':(lambda a,b:a&b),'OR':(lambda a,b:a|b),'XOR':(lambda a,b:a^b),'NOT':(lambda a:a^1)}
g={'0':(lambda:0),'1':(lambda:1)}
d=[(g[v[0]],[]) if o=='IN' else (f[o],[int(a)-1 for a in v]) for o,*v in map(str.split,sys.stdin.read().strip().split('\n'))]
q=lambda i:d[i][0](*(v[x] for x in d[i][1]))
t=[0]
for i in t:t.extend(d[i][1])
v=[0]*n
for i in t[::-1]:v[i]=q(i)
f=[0]*n
f[0]=1
for i in t:
 if f[i]:
  for k in d[i][1]:
   v[k]^=1
   f[k]=q(i)!=v[i]
   v[k]^=1
print(''.join(str(f[i]^v[0]) for i in range(n) if not d[i][1]))

import sys
n=int(input())
f={'AND':(lambda a,b:a&b),'OR':(lambda a,b:a|b),'XOR':(lambda a,b:a^b),'NOT':(lambda a:a^1)}
g={'0':(lambda:0),'1':(lambda:1)}
d=[(g[v[0]],[]) if o=='IN' else (f[o],[int(a)-1 for a in v]) for o,*v in map(str.split,sys.stdin.read().strip().split('\n'))]
q=lambda i:d[i][0](*(v[x] for x in d[i][1]))
t=[0]
for i in t:t.extend(d[i][1])
v=[0]*n
for i in t[::-1]:v[i]=q(i)
f=[0]*n
f[0]=1
for i in t:
 if f[i]:
  for k in d[i][1]:
   v[k]^=1
   f[k]=q(i)!=v[i]
   v[k]^=1
print(''.join(str(f[i]^v[0]) for i in range(n) if not d[i][1]))

import sys
n = int(input())
g = {
    'A': lambda a, b: v[a] & v[b],
    'O': lambda a, b: v[a] | v[b],
    'X': lambda a, b: v[a] ^ v[b],
    'N': lambda a: v[a] ^ 1,
    'I': lambda a: a + 1
}
def f(q):
    q = q.split()
    return q.pop(0)[0], [int(t) - 1 for t in q]
d = [f(q) for q in sys.stdin.readlines()]
t = [0]
for i in t:
    f, a = d[i]
    if f != 'I': t.extend(a)
v = [0] * n
for i in t[::-1]:
    f, a = d[i]
    v[i] = g[f](*a)
s = [0] * n
s[0] = 1
for i in t:
    if not s[i]: continue
    f, a = d[i]
    if f == 'I': continue
    for k in a:
        v[k] ^= 1
        s[k] = g[f](*a) != v[i]
        v[k] ^= 1
print(''.join(str(q ^ v[0]) for q, (f, a) in zip(s, d) if f == 'I'))

# https://codeforces.com/problemset/problem/1010/D
# TLE
import sys
input=sys.stdin.readline

def handle(type_, val_, u, g, S):
    if type_   == 'NOT':
        S.append(g[u][0])
    else:
        v1, v2     = g[u] 
        val1, val2 = Val[v1], Val[v2] 
        
        if oper[type_](1-val1, val2) != val_:
            S.append(v1)
            
        if oper[type_](val1, 1-val2) != val_:
            S.append(v2)       
 
def xor_(a, b):
    return a ^ b
 
def or_(a, b):
    return a | b
 
def not_(a):
    return 1^a
 
def and_(a, b):
    return a&b
 
g={}
 
# {key: [type, val]}
 
def push(d, u, v):
    if u not in d:
        d[u]=[]
    d[u].append(v)
 
n = int(input())
Val  = [None]*n
Type = ['']*n
 
 
for i in range(n):
    arr = input().split()
    
    if len(arr)==2:
        if arr[0]=='IN':
            Type[i] = 'IN'
            Val[i]  = int(arr[1])
        else:
            Type[i]=arr[0]
            push(g, i, int(arr[1])-1)
    else:
        type_, v1, v2 = arr[0], int(arr[1]), int(arr[2])  
        Type[i]=type_ 
        push(g, i, v1-1)
        push(g, i, v2-1)
        
oper={}
 
oper['XOR']=xor_
oper['OR']=or_
oper['NOT']=not_
oper['AND']=and_    
 
S=[0]
i=0
while i<len(S):
    u=S[i]
    if u in g:
        for v in g[u]:
            S.append(v)
    i+=1    
    
for u in S[::-1]:
    if u in g:
        type_ = Type[u]
        
        if len(g[u])==1:
            val_   = Val[g[u][0]]
            Val[u] = oper[type_](val_)
        else:
            val_1, val_2  = Val[g[u][0]], Val[g[u][1]]
            Val[u] = oper[type_](val_1, val_2)
            
ans= [0]*n
S  = [0]
i  = 0
 
while i<len(S):
    u=S[i]
    
    if u in g:
        type_, val_ = Type[u], Val[u]
        handle(type_, val_, u, g, S)
        
    i+=1
    
root_val = Val[0]    
ans      = [root_val]*n
 
for x in S:
    if Type[x]=='IN':
        ans[x]=1-ans[x]
        
print(''.join([str(ans[x]) for x in range(n) if Type[x]=='IN'] ))