Higher order cohomological restrictions on anomalies and counterterms