A generalized Dedekind-Mertens lemma and its converse