Statistical mechanics with topological constraints: I