A sex-specific switch between visual and olfactory inputs underlies adaptive sex differences in behavior