Joint distribution with specified marginals

Suppose we are given a probability distribution over a finite discrete product space $p(x,y)$ with marginals $p(x), p(y) > 0$ for each $x,y$ respectively. We are given two more marginal distributions $r(x), r(y)>0,$ for each $x,y$ respectively. Can we always find functions $f(x), g(y)$ such that

$\sum_y p(x,y)f(x)g(y) = r(x)$

$\sum_x p(x,y)f(x)g(y) = r(y)$?

It appears that we should always be able to do this, but I would like an explicit expression for a solution $f,g$ in terms of $r(x), r(y).$ Thanks.

There are some notational issues. The two different marginals and the joint distribution for X and Y need to be distinguished by different choice of symbols. My suggestions are: $p(x,y), p_X(x), p_Y(y)$, and similarly $r_X(x), r_Y(y)$. Now your question is existence of $f(x), g(y)$ such that $\sum_y p(x,y) p_X(x) g(y) = r_X(x),$ and similarly the other one. As the LHS is a summation over $y$, it must be a function purely one $x$.
Now using your hypothesis that you are in finite discrete space the existence of $f(x), g(y)$ can be interpreted as a question of existence of solutions to two linear system of equations.