This is a problem I met on a Chinese online judge six years ago.

~~I'm not sure if it is a problem from another place.~~

It's SGU 420 -- Number Permutations from 2007-2008 Summer Petrozavodsk Camp, Petr Mitrichev Contest 3. (Thanks to Claris.)

Here's the problem:

**Two different positive integers are considered similar if the numbers of their digits are the same, and the multisets of their digits are the same as well.**

**Given integers $$$l$$$ and $$$r$$$, count the number of integers $$$x$$$ ($$$l \leq x \leq r$$$) such that there is exactly one integer $$$y$$$ ($$$l \leq y \leq r$$$, $$$x \neq y$$$) satisfying that $$$x$$$ and $$$y$$$ are similar.**

**$$$1 \leq l \leq r \leq 10^{15}$$$**

A few random examples of similar numbers:

- 6892, 6928, 6982, 8269 are similar
- 5986656, 5986665, 6556689, 6556698 are similar
- 2983321011, 2983321101, 2983321110, 3011122389 are similar
- 797776555322101, 797776555322110, 901122355567777, 901122355576777 are similar

**UPD:** I've solved this problem. I'll soon elaborate my solution in the comment.