Yes the products are often made in the same factories but use different ingredient specs eg biscuits for M&S will contain butter / free range whole egg and biscuits made for other retailers will contain marg / powder egg.
I visited a farm packing jersey royal potatoes: M&S rejected >75% of the potatoes and Lidl only got to pack the rejects. So yes same factories and growers but very different product specs.
I work in this area from time to time - Believe me there are massive discrepancies between the retailers in terms of product quality, innovation, R&D, CSR etc. M&S are heads and shoulders above the other mainstream retailers and you do get what you pay for. If I could afford to shop there, I would.