Has the phone been dropped or gotten wet at all?
If not or even if it has then the phone will have ot be opened and tested to find out whether it is a problem either with the touchscreen or perhaps the systemboard.
Here's an image of the touchscreen troubleshooting flowchart and also a zoomed in and a board level image showing the location of the components mentioned in the flowchart.
(click on image)
If it is determined that the touchscreen is faulty then the display assembly will have to be replaced as it is one complete module i.e. combined Amoled screen and digitzier screen (touchscreen), not two separately replaceable parts.
Here's the ifixit Samsung Galaxy M31 Vervaing van het AMOLED-scherm en de digitizer that will help.
Search online for GH82-22631A GH82-22405A to find suppliers of the display, that suit you best.
If doing this yourself is too daunting, contact a reputable, professional mobile phone repair service and ask for a quote to repair the phone.