def remove_terminal(node, dag: dict, parents: set=None):
    """Modify dag in-place, removing given node as if it was a terminal"""
    if not parents:
        parents = set()
        for pred, succs in dag.items():
            if node in succs:
                parents.add(pred)
    for parent in parents:
        dag[parent].remove(node)
    if node in dag:
        del dag[node]
