<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:

def serialize_endnote(ctx, document, el, root):
    "Serializes endnotes."

    footnote_num = el.rid

    if el.rid not in ctx.endnote_list:
        ctx.endnote_id += 1
        ctx.endnote_list[el.rid] = ctx.endnote_id

    footnote_num = ctx.endnote_list[el.rid]

    note = etree.SubElement(root, 'sup')
    link = etree.SubElement(note, 'a')
    link.set('href', '#')
    link.text = u'{}'.format(footnote_num)

    fire_hooks(ctx, document, el, note, ctx.get_hook('endnote'))

    return root