Gravitational theories with stable (anti-)de Sitter backgrounds