I have tried it using both adjacency matrix and dictionary but both are giving run time error on TC 3.Can anyone tell me what the problem in the code? ~~~~~ import sys sys.stdin=open('input.txt','r') sys.stdout=open('output.txt','w') import math import heapq def input(): return sys.stdin.readline().strip("\n") def I(): return (input()) def II(): return (int(input())) def MI(): return (map(int,input().split())) def LI(): return (list(map(int,input().split()))) def isPerfectSquare(x): return (int(math.sqrt(x))**2 == x) MOD = 10**9 + 7 def dfs(i): visited[i] = 1 for l in graph[i]: if visited[l] == 0: dfs(l) for _ in range(II()): n = II() a = LI() b = LI() graph = {} visited = [0]*(n+1) for i in range(n): if a[i] not in graph: graph[a[i]] = [b[i]] # print(graph) ans = 1 for i in range(1,n): if visited[i] == 0: dfs(i) ans = (ans * 2) % MOD print(ans)
~~~~~