def transition_model(corpus, page, damping_factor):
    """
    Return a probability distribution over which page to visit next,
    given a current page.

    With probability `damping_factor`, choose a link at random
    linked to by `page`. With probability `1 - damping_factor`, choose
    a link at random chosen from all pages in the corpus.
    """
    ans = {}
    for val in [*corpus]:
        ans[val] = 0
    n = len(corpus[page])
    if n != 0:
        one_from_page = damping_factor / n
        for val in [*corpus[page]]:
            ans[val] += one_from_page
        N = len(corpus)
        for val in [*corpus]:
            ans[val] += (1 - damping_factor) / N
    else:
        N = len(corpus)
        if N != 0:
            for val in [*corpus]:
                ans[val] += (1) / N
    return ans