Let g(x,y)=ax+by+c and h(x,y)=dx+ey+f.
gh=adx^2+aexy+afx+bdxy+bey^2+bfy+cdx+cey+cf=adx^2+(ae+bd)xy+bey^2+(af+cd)x+(bf+ce)y+cf.
cf=p; ad=3; ae+bd=5; be=-2; af+cd=-5; bf+ce=4.
Let a=1 then d=3; e+3b=5; b=-2/e, so e-6/e=5, e^2-6=5e, e^2-5e-6=0=(e-6)(e+1), so e=6 or -1 making b=-1/3 or 2.
Assume e=-1 and b=2, because a fraction seems unlikely. 2f-c=4 and cf=p, so c=p/f and 2f-p/f=4, 2f^2-4f-p=0.
However, we haven't used af+cd=-5 yet. We can substitute values we already know: f+3c=-5 and c=p/f, so f+3p/f=-5, f^2+5f+3p=0. We have two equations containing f and p. p=2f^2-4f, so f^2+5f+6f^2-12f=0, 7f^2-7f=0, so f=0 or 1. We exclude f=0 because c=p/f and we can't divide by zero, so f=1 and c=p. 3c=-5-f=-6 so c=-2=p.
Therefore f(x,y)=3x^2+5xy-2y^2-5x+4y-2 and the linear functions are g(x,y)=x+2y-2 and h(x,y)=3x-y+1.
f(x,y)=(x+2y-2)(3x-y+1).