import collections


def get_rare_char_info(char_to_lang_map, shared_lang_threshold):
    """Returns a tuple of:
  - a set of 'rare_chars' (those used threshold langs or fewer),
  - a mapping from each locale with rare chars to a set of its rare chars"""

    rare_chars = set()
    locs_with_rare_chars = collections.defaultdict(set)
    for cp in char_to_lang_map:
        num_shared_langs = len(char_to_lang_map[cp])
        if num_shared_langs <= shared_lang_threshold:
            rare_chars.add(cp)
            for lang_tag in char_to_lang_map[cp]:
                locs_with_rare_chars[lang_tag].add(cp)
    return rare_chars, locs_with_rare_chars