def transition_model(corpus, page, damping_factor):
    """
    Return a probability distribution over which page to visit next,
    given a current page.
   """
    model= dict()
    outgoing_links= corpus[page]
    count_outgoing_links= len(outgoing_links)
    #if a page is chosen at random from the corpus
    p1 = (1-damping_factor)/len(corpus)
    #if a page is chosen from the given page
    if count_outgoing_links!=0:
        p2= (damping_factor)/(count_outgoing_links) 
    else: # a page having no links is interpreted as one having link to every page inc itself
        p2= (damping_factor)/len(corpus)

    for page in corpus:
        if count_outgoing_links==0:
            model[page]= p1+ p2
        elif page in outgoing_links:
            model[page] = p1 + p2
        else:
            model[page] = p1

    return model