Behavioral flexibility, the ability to adjust behavioral strategies in response to changing environmental contingencies and internal demands, is fundamental to cognitive functions. Despite a large body of pharmacology and lesion studies, the underlying neurophysiological correlates and mechanisms that support flexible rule switching remain elusive. To address this question, we trained mice to distinguish complex sensory cues comprising different perceptual dimensions (set shifting). Endoscopic calcium imaging revealed that medial prefrontal cortex (mPFC) neurons represented multiple task-related events and exhibited pronounced dynamic changes during rule switching. Notably, prominent encoding capacity in the mPFC was associated with switching across, but not within perceptual dimensions. We then showed the involvement of the ascending modulatory input from the locus coeruleus (LC), as inhibiting the LC impaired rule switching behavior and impeded mPFC dynamic processes and encoding. Our results highlight the pivotal role of the mPFC in set shifting processes and demonstrate the profound impact of ascending neuromodulation on shaping prefrontal neural dynamics and behavioral flexibility.