Is there a formula to calculate all possible unique permutations of n elements over p positions? Is there a formula to calculate all possible unique permutations of n elements over p positions?
Please imagine the following scenario:
I have p positions (cells/spaces) to fill each with one element, let's have use letters as elements for example.
I have n letters in total and there may be some duplicate ones among them but I know in advance how many unique letters I have, s, and how many duplicates I have for each letter (0 or more).
So, resuming the situation, we have:
p = number of positions (cells/spaces to be filled with one element each);
n = number of total elements available (we'll take letters);
s = number of single symbols (= the total number of elements not counting duplicated ones, so let's have S is the set containing these symbols, s is its cardinality: s=|S|);
Obvious rule here is:
n >= p >= 1

and
n >= s >= 1

('cause s is a sort of subset of the n elements without duplicates, n = s when there's no duplicated elements.)
Then we can have Oi, with i = 1...s, to know either the number of
 duplicates (0 <= Oi < n) -OR- instances (1 <= Oi <= n)
of every S symbol (free choice on this, even I prefer the latter approach).
As a clarification case let's consider the letters that form the word "danicotra" and suppose we have 9 free spaces to fill with them.
In this case we will have: p = 9, n = 9 and s = 8
 (s is 8 because there are two "a" in "danicotra" so we have 9 elements (n) but only 8 single symbols (s) keeping the duplicate elements aside);
the "symbols set" is S={d,a,n,i,c,o,t,r}
and therefore we'll have the following 8 single symbols:
Symbol1 = "d"
Symbol2 = "a"
Symbol3 = "n"
Symbol4 = "i"
Symbol5 = "c"
Symbol6 = "o"
Symbol7 = "t"
Symbol8 = "r"

and Oi (with i = 1...8), if we count number of duplicates per symbol, like this:
O1=0
O2=1
O3=0
O4=0
O5=0
O6=0
O7=0
O8=0

or if we count number of instances per symbol (that is my favored), like this:
O1=1
O2=2
O3=1
O4=1
O5=1
O6=1
O7=1
O8=1

Ok, now that we took the above as sample case (and I described what p, n, s, S and Oi are like in this situation), let's get back to the question: is there a general formula I can apply to know the number of the possible unique(*) permutations with it?
(*) unique = I mean, 'cause I might happen having repetitions/duplicates if a same "symbol" is present more than once amongst the elements and I don't want to take 'em into account (see image below for example)

EDIT:
There's a general formula that works only "partially":
$$
\frac{n!}{(n-p)! \ (\prod_{i=1}^s O_i!)}
$$
BUT, as I just said, it only works "partially", that is:


*

*it gives correct results only if I have no duplicated symbols amongst
n elements (n = s) OR when I have (n-1) <= p <= n,

*while it fails when I have duplicated elements amongst n symbols (1 <= s < n) AND I have 1 <= p < (n-1).


In other words, it works with the above example with the word "danicotra" just because I have 9 elements and 9 positions to fill (n = p) but if I need to calculate the same thing with, for instance, p = 7 ... that's the pain! :(
So far I'm still looking for the right formula, that works always.
Thanks for help
 A: I will try to give an answer, altough the formula is not as nice, as I would like it to be, but I have no idea, how to simplify it further. Also yesterday I made an attempt for the formula. There the final "correction" was wrong, since it is not so trivial as I thought it will be. 
Hopefully the correct answer:
Let $K_i$ be, what you denoted by $Oi$ (in your second interpretation, meaning, that $K_i$ is always greater than zero).
In a first step fix values of $k_i$ such, that $0\le k_i \le K_i$ and $\sum_{i=1}^s k_i=p$.
How many permutations exist, such that symbol 1 appears $k_1$ times, symbol 2 appears $k_2$ times and so on. The answer is $p!$. 
We don't want all $p!$ permutations, but want to correct for the fact, that symbols may appear more than once in the sequence. 
This results in $\frac{p!}{k_1!\cdots k_s!}$ sequences.
Now we want to add up these sequences
\begin{equation}
\sum_{k_1=0}^{K_1}\cdots\sum_{k_s=0}^{K_s}\mathbb{1}_{p}(k_1+\cdots+k_s)\frac{p!}{k_1!\cdots k_s!},
\end{equation} 
where $\mathbb{1}_{p}(k_1+\cdots+k_s) $ is the indicator function, that is 1, if the argument is equal to $p$ and else zero.
Hope this is helpful
Edit: Add rudimentary R code, with the example "mississippi" hardcoded. Corresponds with the numbers of recursive solution of whuber
K1 <- 1
K2 <- 4
K3 <- 4
K4 <- 2

p <- 10

iter <- 0
iter2 <- 0
res <- 0
tic <- Sys.time()
for(k1 in 0:K1) {
  for(k2 in 0:K2) {
    for(k3 in 0:K3) {
      for(k4 in 0:K4) {
        iter <- iter + 1
        if (k1 + k2 + k3 + k4 == p) {
          iter2 <- iter2 + 1
          res <- res + factorial(p)/(factorial(k1)*factorial(k2)*factorial(k3)*factorial(k4))
          break
        }
        else if (k1 + k2 + k3 >= p) break
      }
    }
  }
}
toc <- Sys.time()
(toc - tic)
res

Wrong answer:
The number of all possible permutations of $n$ elements is $n!$.
But you are only interested in the first $p$ positions of these permutations, so for each sequence of the first $p$ elements, you have $(n-p)!$ irrelevant duplicates, therefore you correct for this and you end up with $\frac{n!}{(n-p)!}$ possibilities.
Still you count irrelevant duplicates, due to the duplication of letters. You have to correct for that.
Let me define a new notation $k_i$ is what you denoted as 0i (with your latter interpretation, that means k_i is never zero). 
The final formula than is: $\frac{n!}{(n-p)!k_1!\cdots k_s!}$.
A: Solutions to a couple of famous problems of this type:
Arrangements of letters in MISSISSIPPI:
$11!/(4!\cdot 4! \cdot 2!).$
And in STATISTICS:
$10!/(3!\cdot 3! \cdot 2!).$
Also, ${10 \choose 3}{7 \choose 3}{4 \choose 2}2!.$
Relevant to binomial PDF: Arrangements of letters in SSSFF:
$5!/(3!\cdot 2!) = {5 \choose 2}.$
