from collections import defaultdict

def target_sum_ways(nums, target):
    counts = {0: 1}
    for value in nums:
        next_counts = defaultdict(int)
        for total, count in counts.items():
            next_counts[total + value] += count
            next_counts[total - value] += count
        counts = dict(next_counts)
    return counts.get(target, 0)
