An exact formula for an average of $L$-series