Probably an intake manifold vacuum leak with the additional possibility of a tired upstream O2 sensor on the firewall side.
The rough running condition plus the P0171 code are very strong indicators of the vacuum leak.
The P1131 may be due to the vacuum leak.
I would address the intake first, reset everything, then see if the P1131 comes back before replacing the sensor. OTOH, if the O2 sensors are original, I'd probably replace them anyway as a maintenance item.
Steve