Counting Spanning Trees in Cographs: An Algorithmic Approach