Kinetics of intramolecular and intermolecular reactions involving two functional groups attached to polymers