def triangles(creation_sequence):
    """
    Compute number of triangles in the threshold graph with the
    given creation sequence.
    """
    cs = creation_sequence
    dr = cs.count('d')
    ntri = dr * (dr - 1) * (dr - 2) / 6
    for i, typ in enumerate(cs):
        if typ == 'i':
            ntri += dr * (dr - 1) / 2
        else:
            dr -= 1
    return ntri
