When rolling a die $N$ times, what is the probability of an $X$ appearing before the last $Y$? So a valid state is when there are both $X$ and $Y$ in the roll sequence, and the lowest index of an $X$ is smaller than the biggest index of a $Y$.
In particular, if after $N$ rolls, there is no $X$ or no $Y$, this does not count as a success.
For example
$N = 5$, $X = 1$, $Y = 2$ (Roll dice 5 times, 1 appearing before 2)

*

*1 4 2 3 1


*2 1 3 4 2
I would like the formula such that we can calculate the probability given $N$, $p_X$ and $p_Y$.
The closest I got is this :
def prob(N,pX,pY):
    res = 0
    for i in range(0,N):
        res+=((1-pX)**i)*pX*(N-i-1)*pY
    return res

 A: Here you go:
$$ \sum_{k=1}^{N-1}(1-p_Y)^{k-1}p_Y\big(1-(1-p_X)^{N-k}\big). $$
I sure hope that makes you happy. probabilityislogic turned this into the following only-slightly-less-intimidating closed form:
$$
1-(1-p_X)^{N-1}-p_X\big[(1-p_Y)^{N-1}-(1-p_X)^{N-1}\big]\frac{1-p_Y}{p_X-p_Y} \quad\text{if }p_X\neq p_Y
$$
$$
1-(1-p+Np)(1-p)^{N-1}\quad\text{if }p_X=p_Y=:p.
$$
The first idea is that the probability of a "successful" outcome ("both $X$ and $Y$ are present in the roll, and an $X$ appears before the last $Y$") is equivalent to "both $X$ and $Y$ are present in the roll, and an $X$ appears after the first $Y$". The equivalence lies simply in counting from the back instead of from the front. (This just simplifies the summation indices above, you can easily work with the original formulation, it's just a little messier.)
Let's work with this equivalent formulation. It can be disentangled as:


*

*there is some index $k\in\{1, \dots, N-1\}$ (this is what we sum over) such that:

*there is no $Y$ for the $k-1$ first rolls (this is the $(1-p_Y)^{k-1}$ term) and

*the $k$-th roll is $Y$ (with probability $p_Y$) and

*there is at least one $X$ roll after that (this is the $\big(1-(1-p_X)^{N-k}\big)$ term: it's the complement of there being no $X$ in $N-k$ rolls).


The formula seems to work in simulations, where I played around with the nn, px and py parameters (in R):
nn <- 2
px <- .3
py <- .3

n_sims <- 1e4
n_x_before_y <- 0

for ( ii in 1:n_sims ) {
    roll <- sample(x=c("x","y","z"),size=nn,replace=TRUE,prob=c(px,py,1-px-py))
    if ( "x" %in% roll & "y" %in% roll) {
        if ( min(which(roll=="x"))<max(which(roll=="y")) ) {
            n_x_before_y <- n_x_before_y+1
        }
    }
}

n_x_before_y/n_sims


sum((1-py)^(0:(nn-2))*py*(1-(1-px)^((nn-1):1)))

if ( isTRUE(all.equal(px,py)) ) {
    cat(1-(1-px+nn*px)*(1-px)^(nn-1),"\n")
} else {
    cat(1-(1-px)^(nn-1)-px*((1-py)^(nn-1)-(1-px)^(nn-1))*(1-py)/(px-py),"\n")
}

This has been giving me the correct result, both for the sum form and for the closed form probabilityislogic proposed.
Of course, it does not matter whether the die is 3-, 6- or 27-sided - all we care about is the probabilities of the two events in question and "everything else", so for our purposes, we can collapse "everything else".
An interesting aspect that stumped me for a while is that the specific probabilities $p_X$ and $p_Y$ have a different impact for small and large $N$. If $N$ is small, e.g., $N=2$, then all we care is whether both $X$ and $Y$ are present in the roll at all. Conditional on that, $(X,Y)$ and $(Y,X)$ are equally likely, no matter how different $p_X$ and $p_Y$ are! But if $N$ grows, then both $X$ and $Y$ are more and more certainly present in the overall roll, and which one appears first depends of course on the ratio between $p_X$ and $p_Y$.
Overall, a nice little problem. Thanks!
